標籤:
一、七牛自訂配置節點
<configSections> <section name ="QiniuConfig" type="Amy.Toolkit.QiniuStorage.SectionHandler"/> </configSections> <QiniuConfig> <add key="AccessKey" value="自己的accesskey"></add> <add key="SecretKey" value="自己的secretkey"></add> <add key="Bucket" value="空間名稱"></add> <add key="Domain" value="七牛網域名稱/自己網域名稱"></add> </QiniuConfig>
二、實現自訂配置功能
public class Config { /// <summary> /// 七牛公開金鑰 /// </summary> public string AccessKey { get; set; } /// <summary> /// 七牛密鑰 /// </summary> public string SecretKey { get; set; } /// <summary> /// 資源空間 /// </summary> public string Bucket { get; set; } /// <summary> /// 空間網域名稱 /// </summary> public string Domain { get; set; } } public class SectionHandler : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { Config config = new Config(); //解析設定檔資訊,返回對象 if (section != null) { foreach (XmlNode item in section.ChildNodes) { switch (item.Attributes["key"].InnerText) { case "AccessKey": config.AccessKey = item.Attributes["value"].InnerText; break; case "SecretKey": config.SecretKey = item.Attributes["value"].InnerText; break; case "Bucket": config.Bucket = item.Attributes["value"].InnerText; break; case "Domain": config.Domain = item.Attributes["value"].InnerText; break; default: break; } } } return config; } }
View Code
三、實現七牛上傳圖片功能
public class QiniuFactory { /// <summary> /// 上傳圖片 /// </summary> /// <param name="stream">圖片流</param> /// <returns>七牛圖片路徑</returns> public static string UploadImage(System.IO.Stream stream) { Config config = (Config)ConfigurationManager.GetSection("QiniuConfig"); //設定帳號的AK和SK Qiniu.Conf.Config.ACCESS_KEY = config.AccessKey; Qiniu.Conf.Config.SECRET_KEY = config.SecretKey; IOClient target = new IOClient(); PutExtra extra = new PutExtra(); //設定上傳的空間 String bucket = config.Bucket; //設定上傳的檔案的key值(及檔案名稱) String key = IDHelper.Id32 + ".jpg"; //普通上傳,只需要設定上傳的空間名就可以了,第二個參數可以設定token到期時間 PutPolicy put = new PutPolicy(bucket, 3600); //調用Token()方法產生上傳的Token string upToken = put.Token(); //上傳檔案的路徑 //string filePath = @"F:\130326213588cbde762953a234.jpg"; //調用PutFile()方法上傳 //PutRet ret = target.PutFile(upToken, key, filePath, extra); var ret = target.Put(upToken, key, stream, extra); return string.IsNullOrEmpty(ret.key) ? string.Empty : string.Format("{0}/{1}", config.Domain, ret.key); } /// <summary> /// 上傳圖片 /// </summary> /// <param name="stream">圖片流</param> /// <returns>七牛圖片路徑</returns> public static string UploadImage(byte[] bytes) { Config config = (Config)ConfigurationManager.GetSection("QiniuConfig"); //設定帳號的AK和SK Qiniu.Conf.Config.ACCESS_KEY = config.AccessKey; Qiniu.Conf.Config.SECRET_KEY = config.SecretKey; IOClient target = new IOClient(); PutExtra extra = new PutExtra(); //設定上傳的空間 String bucket = config.Bucket; //設定上傳的檔案的key值(及檔案名稱) String key = IDHelper.Id32 + ".jpg"; //普通上傳,只需要設定上傳的空間名就可以了,第二個參數可以設定token到期時間 PutPolicy put = new PutPolicy(bucket, 3600); //調用Token()方法產生上傳的Token string upToken = put.Token(); //上傳檔案的路徑 //string filePath = @"F:\130326213588cbde762953a234.jpg"; //調用PutFile()方法上傳 //PutRet ret = target.PutFile(upToken, key, filePath, extra); var ret = target.Put(upToken, key, new System.IO.MemoryStream(bytes), extra); return string.IsNullOrEmpty(ret.key) ? string.Empty : string.Format("{0}/{1}", config.Domain, ret.key); } }
View Code
四、用戶端調用七牛
/// <summary> /// 七牛上傳檔案 /// </summary> public string QiniuUploadFile() { var url = "七牛圖片訪問路徑"; var files = Request.Files; if (files.Count > 0) { url = Amy.Toolkit.QiniuStorage.QiniuFactory.UploadImage(files[0].InputStream); } return url; }
七牛圖片雲端儲存 配置及樣本