using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; using Vinno.vCloud.Common.Storage.ObjectStorageInfo; namespace Vinno.vCloud.Disk.UFile { public class UploadHelper { private static StorageUploadConfig _cloudStorageConfig; public static void Init() { var ufileUploaderConfig = "{\n" + " \"UrlBucket\": \"" + "http://flyinsono-test.cn-bj.ufileos.com/" + "\",\n" + " \"StorageUrl\": \"\",\n" + " \"Heads\": {\n" + " \"UserAgent\": {\n" + " \"ProductInfoHeaderValue\": {\n" + " \"ProductName\": \"UCloudCSharp\",\n" + " \"Version\": \"1.0.2\"\n" + " }\n" + " },\n" + " \"RequestHeads\": [\n" + " {\n" + " \"HeadKey\": \"X-Ufile-Storage-Class\",\n" + " \"HeadValue\": \"Standard\"\n" + " }\n" + " ]\n" + " }\n" + "}\n"; _cloudStorageConfig = JsonConvert.DeserializeObject(ufileUploaderConfig); } /// /// 下载文件 /// /// 文件下载地址 /// public static byte[] DownloadFile(string requestUrl) { return ObjectStorageSingle.Instance.GetFile(requestUrl); } /// /// 上传文件 /// /// /// /// /// public static string UploadFile(byte[] fileData,string fileName, string mimeType, Action progressCallback = null) { var contentType = MediaTypeHeaderValue.Parse(mimeType); var requestHeads = new Dictionary(); ProductInfoHeaderValue productInfoHeaderValue = null; var authorization = GetAuthorization(fileName); requestHeads.Add("Authorization", authorization); var requestUrl = _cloudStorageConfig.UrlBucket + fileName; var heads = _cloudStorageConfig.Heads; if (heads != null) { var httpHeads = heads.RequestHeads; foreach (var httpHead in httpHeads) { requestHeads.Add(httpHead.HeadKey, httpHead.HeadValue); } var userAgent = heads.UserAgent; if (userAgent != null) { var productInfoHeader = userAgent.ProductInfoHeaderValue; productInfoHeaderValue = new ProductInfoHeaderValue(productInfoHeader.ProductName, productInfoHeader.Version); } } var result = ObjectStorageSingle.Instance.UploadFile(fileData, contentType, requestUrl, requestHeads, productInfoHeaderValue, progressCallback); if (result) { return requestUrl; } return string.Empty; } /// /// 删除文件 /// /// /// /// public static bool DeleteFile(string fileName) { var authorization = GetAuthorization(fileName); var requestUrl = _cloudStorageConfig.UrlBucket + fileName; return ObjectStorageSingle.Instance.DeleteFile(requestUrl, authorization); } private static string GetAuthorization(string fileName) { var contentType = string.Empty; if (fileName.LastIndexOf(".") > -1) { contentType = HttpClientHelper.GetMimeType(fileName.Substring(fileName.LastIndexOf("."))); } var authorization = string.Empty; authorization += "UCloud "; authorization += "TOKEN_2840e067-0874-4c72-81d3-baa43c7d510d"; authorization += ":"; var stringToSign = HttpMethod.Put.Method + "\n"; stringToSign += "\n"; stringToSign += contentType; stringToSign += "\n"; stringToSign += "\n"; stringToSign += $"/flyinsono-test/" + fileName; var hmac = new HMACSHA1(Encoding.ASCII.GetBytes("fd099d8b-200c-4a37-9b91-27875b0340e5")); var hashValue = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); var signature = Convert.ToBase64String(hashValue); return authorization + signature; } } }