腾讯云对象存储接入
方案
-
使用腾讯云对象存储API进行操作
- 优点:逻辑编写自由度高
- 缺点:调用较SDK复杂,需自行组装数据
// https://cloud.tencent.com/document/product/436/7778 public class CosAuthorization { private const string Algorithm = "sha1"; private const string Host = "service.cos.myqcloud.com"; private const string RequestMethod = "get"; private const string RequestPath = "/"; private readonly string SecretId; private readonly string SecretKey; private readonly ILogger<CosAuthorization> _logger; public CosAuthorization(string secretId, string secretKey, ILogger<CosAuthorization> logger) { SecretId = secretId; SecretKey = secretKey; _logger = logger; } public string ToHMAC_SHA1(string key, string value) { byte[] btKey = Encoding.UTF8.GetBytes(key); byte[] btValue = Encoding.UTF8.GetBytes(value); using (HMACSHA1 hmacsha1 = new HMACSHA1(btKey)) { byte[] hashBytes = hmacsha1.ComputeHash(btValue); return BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); } } public string ToSHA1(string value) { using (SHA1 sha1 = SHA1.Create()) { byte[] btValue = Encoding.UTF8.GetBytes(value); byte[] hashBytes = sha1.ComputeHash(btValue); return BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); } } public string MakeAuthorization() { try { TimeSpan timeSpan = DateTime.UtcNow - DateTime.UnixEpoch; long nStartTimetamp = Convert.ToInt64(timeSpan.TotalSeconds); long nEndTimetamp = nStartTimetamp + 3600; string szKeyTime = $"{nStartTimetamp};{nEndTimetamp}"; string szUrlParamList = string.Empty; string szHttpParameters = string.Empty; string szHeaderList = "host"; string szHttpHeaders = $"host={HttpUtility.UrlEncode(Host)}"; string szSignKey = ToHMAC_SHA1(SecretKey, szKeyTime); string szHttpString = $"{RequestMethod}\n{RequestPath}\n{szHttpParameters}\n{szHttpHeaders}\n"; string szStringToSign = $"{Algorithm}\n{szKeyTime}\n{ToSHA1(szHttpString)}\n"; string szSignature = ToHMAC_SHA1(szSignKey, szStringToSign); string szAuthorization = $"q-sign-algorithm={Algorithm}&q-ak={SecretId}&q-sign-time={szKeyTime}&q-key-time={szKeyTime}&q-header-list={szHeaderList}&q-url-param-list={szUrlParamList}&q-signature={szSignature}"; // 使用日志记录代替 Console.WriteLine _logger.LogInformation($"szKeyTime = {szKeyTime}"); _logger.LogInformation($"szUrlParamList = {szUrlParamList}"); _logger.LogInformation($"szHttpParameters = {szHttpParameters}"); _logger.LogInformation($"szHeaderList = {szHeaderList}"); _logger.LogInformation($"szHttpHeaders = {szHttpHeaders}"); _logger.LogInformation($"szSignKey = {szSignKey}"); _logger.LogInformation($"szHttpString = {szHttpString}"); _logger.LogInformation($"szStringToSign = {szStringToSign}"); _logger.LogInformation($"szSignature = {szSignature}"); _logger.LogInformation($"szAuthorization = {szAuthorization}"); return szAuthorization; } catch (Exception ex) { _logger.LogError(ex, "生成Authorization时发生错误"); throw; } } }
-
使用腾讯云对象存储SDK操作
- 优点:调用简单
- 缺点:操作不够细化,导致一些功能无法实现,如分段下载只有高级接口可以实现,但是高级接口只支持将下载文件直接写入本地,而不是stream,会增加一次读写操作
-
结论:先使用SDK以使COS可以快速投入使用,后续功能细化使用API改造
现存的问题
- 使用的是腾讯云单次下载接口,存在下载大小的上限
- 不支持分批下载
- 文件夹和文件未作区分,页面浏览时并未分类显示
- 删除文件时,需要先删除文件,再删除文件夹
- 对于缓存管理较为草率,需要详细规划
- 对于页面的刷新也存在部分问题
后续制作计划
- 将文件夹在页面显示中隐藏
- 调整页面刷新逻辑
- 将整体逻辑调整为API调用