七牛图片云存储 配置及示例
一、七牛自定义配置节点
<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; } }
三、实现七牛上传图片功能
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); } }
四、客户端调用七牛
/// <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; }