標籤:
項目需要將音視頻檔案上傳伺服器,考慮並發要求高,通過七牛來實現。
直接上代碼
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#使用七牛雲端儲存上傳下載檔案