C#使用七牛雲端儲存上傳下載檔案

來源:互聯網
上載者:User

標籤:

項目需要將音視頻檔案上傳伺服器,考慮並發要求高,通過七牛來實現。

直接上代碼

 

using Qiniu.IO;using Qiniu.IO.Resumable;using Qiniu.RPC;using Qiniu.RS;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace qiniuTest{    /// <summary>    /// 檔案上傳有兩種方式:    /// 一種是以普通方式直傳檔案,簡稱普通上傳;    /// 另一種方式是斷點續上傳,斷點續上傳在網路條件很一般的情況下也能有出色的上傳速度,而且對大檔案的傳輸非常友好。    /// </summary>    class Program    {        static void Main(string[] args)        {            Qiniu.Conf.Config.ACCESS_KEY = "6QQ7Cnz4bljdkQOWQ5UOAheVCAd0bCa7Tc5XXXXX";            Qiniu.Conf.Config.SECRET_KEY = "9rUGnbFtvm-PLWcZeOR6ed9MUjZ4bKitf7YXXXXX";            string bucket = "cvtehrmxz";            string fileKey = "應用系統全貌圖.png";            GetFileStat(bucket, fileKey);            //小檔案直傳            string fileName = "業務功能架構圖-IM和企業.jpg";            PutFile(bucket, Guid.NewGuid().ToString() + fileName, "d:\\" + fileName);            //在asp.net mvc中的檔案上傳            //ResumablePutFile(bucket, Guid.NewGuid().ToString(), Path.Combine(path, Request.Form[0]));            //大檔案上傳            //string bigFileName = "eclipse-java-luna-SR1-win32-x86_64.zip";            //ResumablePutFile(bucket, Guid.NewGuid().ToString() + bigFileName, "d:\\Software\\" + bigFileName);            GetFile("7xXXXX.com1.z0.glb.clouddn.com", fileKey);        }        /// <summary>        /// 查看單個檔案屬性資訊        /// </summary>        /// <param name="bucket">七牛雲端儲存空間名</param>        /// <param name="key">檔案key,也就是檔案名稱</param>        public static void GetFileStat(string bucket, string key)        {            RSClient client = new RSClient();            Entry entry = client.Stat(new EntryPath(bucket, key));            if (entry.OK)            {                Console.WriteLine("Hash: " + entry.Hash);                Console.WriteLine("Fsize: " + entry.Fsize);                Console.WriteLine("PutTime: " + entry.PutTime);                Console.WriteLine("MimeType: " + entry.MimeType);                Console.WriteLine("Customer: " + entry.Customer);            }            else            {                Console.WriteLine("Failed to Stat");            }        }        /// <summary>        /// 刪除單個檔案        /// </summary>        /// <param name="bucket">檔案所在的空間名</param>        /// <param name="key">檔案key</param>        public static void Delete(string bucket, string key)        {            Console.WriteLine("\n===> Delete {0}:{1}", bucket, key);            RSClient client = new RSClient();            CallRet ret = client.Delete(new EntryPath(bucket, key));            if (ret.OK)            {                Console.WriteLine("Delete OK");            }            else            {                Console.WriteLine("Failed to delete");            }        }        /// <summary>        /// 大量刪除檔案        /// </summary>        /// <param name="bucket">檔案所在的空間名</param>        /// <param name="keys">檔案key</param>        public static void BatchDelete(string bucket, string[] keys)        {            RSClient client = new RSClient();            List<EntryPath> EntryPaths = new List<EntryPath>();            foreach (string key in keys)            {                Console.WriteLine("\n===> Stat {0}:{1}", bucket, key);                EntryPaths.Add(new EntryPath(bucket, key));            }            client.BatchDelete(EntryPaths.ToArray());        }        /// <summary>        /// 普通方式直傳檔案        /// </summary>        /// <param name="bucket">檔案所在的空間名</param>        /// <param name="key">您可以自行定義檔案Key,一般GUID</param>        /// <param name="fname">檔案路徑+檔案名稱</param>        public static void PutFile(string bucket, string key, string fname)        {            var policy = new PutPolicy(bucket, 3600);            string upToken = policy.Token();            PutExtra extra = new PutExtra();            IOClient client = new IOClient();            client.PutFile(upToken, key, fname, extra);        }        /// <summary>        /// 斷點續上傳方式,傳大檔案用這種方式        /// </summary>        /// <param name="bucket">檔案所在的空間名</param>        /// <param name="key">您可以自行定義檔案Key,一般GUID</param>        /// <param name="fname">檔案路徑+檔案名稱</param>        public static void ResumablePutFile(string bucket, string key, string fname)        {            Console.WriteLine("\n===> ResumablePutFile {0}:{1} fname:{2}", bucket, key, fname);            PutPolicy policy = new PutPolicy(bucket, 3600);            string upToken = policy.Token();            Settings setting = new Settings();            ResumablePutExtra extra = new ResumablePutExtra();            ResumablePut client = new ResumablePut(setting, extra);            client.PutFile(upToken, fname, Guid.NewGuid().ToString());        }        /// <summary>        /// Get方式擷取檔案        /// </summary>        /// <param name="domain">檔案域</param>        /// <param name="key">檔案Key</param>        public static void GetFile(string domain, string key)        {            System.Diagnostics.Process.Start("http://" + domain + "/" + key);        }    }}

 

C#使用七牛雲端儲存上傳下載檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.