七牛圖片雲端儲存 配置及樣本

來源:互聯網
上載者:User

標籤:

一、七牛自訂配置節點

 <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;    }

 

七牛圖片雲端儲存 配置及樣本

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.