C# 檔案上傳到七牛雲伺服器(一)

Kaivin.bao發表於2018-10-31

本章講述:C# 中呼叫七牛雲提供的SDK,實現檔案簡單上傳功能。

分片,斷點續上傳講解,請檢視:C# 檔案上傳到七牛雲伺服器(二)

地址為:

開發流程步驟地址:https://blog.csdn.net/BYH371256/article/details/79868562

1、把下載的“Qiniu.dll”動態庫檔案,引入到專案之中;

2、新增一個類,主要作用是:七牛雲的操作介面,分別新增作用名:Qiniu.Http;//七牛雲http協議名空間、Qiniu.Util;//七牛雲名空間、Qiniu.Storage、Qiniu.CDN

3、其他作用域:System.IO、System.Net

下面請看程式碼:

新建類:QiniuEnv,專門儲存和七牛雲伺服器相關的各項資料

public class QiniuEnv
{

	public string AccessKey;//管理檔案 認證
	public string SecretKey;//管理檔案 認證
	public string Bucket;//空間名,可以是公開或者私有的
	public string Domain;//檔案管理 外鏈預設域名
	public string LocalFile;//本地儲存檔案路徑

	public QiniuEnv()
	{
		//手動把自己註冊七牛雲伺服器的Akey和SKey,名空間、外連結預設域名填上
		this.AccessKey = "";
		this.SecretKey = "";
		this.Bucket = "";//空間名
		this.Domain = "";
		this.LocalFile = "";
	}
}

簡單實現檔案上傳--直接上傳

//七牛雲預設引數初始化
QiniuEnv QiniuParam = new QiniuEnv();

/// 直接上傳可用於資料較小的檔案  圖片
/// <param name="upLoadFile">上傳地址</param>
/// <returns></returns>
public bool UpLoading(string upLoadFile)
{
	bool bresult = false;
	string fileName = System.IO.Path.GetFileNameWithoutExtension(upLoadFile);
	Mac mac = new Mac(QiniuParam.AccessKey, QiniuParam.SecretKey);
	string key = fileName;//檔名稱

	string filePath = upLoadFile;//上傳路徑

	PutPolicy putPolicy = new PutPolicy();
	putPolicy.Scope = QiniuParam.Bucket + ":" + key;
	putPolicy.SetExpires(3600);
	putPolicy.DeleteAfterDays = 1;
	string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
	Config config = new Config();
	config.Zone = Zone.ZONE_CN_South;
	config.UseHttps = true;
	config.UseCdnDomains = true;
	config.ChunkSize = ChunkUnit.U512K;
	FormUploader target = new FormUploader(config);
	HttpResult result = target.UploadFile(filePath, key, token, null);

	string back = result.Code.ToString();
	if (result.Code.ToString() == "200")
		bresult = true;

	return bresult;
}

實現分片上傳

/// <summary>
/// 分片上傳 測試
/// </summary>
/// <param name="upLoadFile">檔案本地地址</param>
/// <returns></returns>
public string ResumableUpLoadingTest(string upLoadFile)
{
	string CallBackUrl = "";
	Mac mac = new Mac(QiniuParam.AccessKey, QiniuParam.SecretKey);
	string FileName = System.IO.Path.GetFileName(upLoadFile);
	string key = FileName;

	string filePath = upLoadFile;
	System.IO.Stream fs = System.IO.File.OpenRead(filePath);

	PutPolicy putPolicy = new PutPolicy();
	putPolicy.Scope = QiniuParam.Bucket + ":" + key;
	putPolicy.SetExpires(3600);
	putPolicy.DeleteAfterDays = 1;
	string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

	Config config = new Config();
	config.UseHttps = true;
	config.Zone = Zone.ZONE_CN_South;//根據地址選擇
	config.UseCdnDomains = true;
	config.ChunkSize = ChunkUnit.U512K;//chunkUnit上傳分片大小,可選值128KB,256KB,512KB,1024KB,2048KB,4096KB
	ResumableUploader target = new ResumableUploader(config);
	PutExtra extra = new PutExtra();
	//設定斷點續傳進度記錄檔案
	extra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey(filePath, key);
	extra.ResumeRecordFile = FileName + ".progress";
	
	HttpResult result = target.UploadStream(fs, key, token, extra);
	Console.WriteLine("resume upload: " + result.ToString());
	if (result.Code == 200)
	{
		CallBackUrl = "http://" + QiniuParam.Domain + "/" + FileName;
		
	}
	return CallBackUrl;
}

 

相關文章