# 网站搭建实录③：腾讯云 COS 对象存储接入

> 发布时间: 2026-08-02 18:26

# 腾讯云对象存储接入

## 方案

### 1. 使用腾讯云对象存储 API 进行操作

1. 优点：逻辑编写自由度高
2. 缺点：调用较 SDK 复杂，需自行组装数据

```csharp
// 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}";

            _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;
        }
    }
}
```

### 2. 使用腾讯云对象存储 SDK 操作

1. 优点：调用简单
2. 缺点：操作不够细化，导致一些功能无法实现，如分段下载只有高级接口可以实现，但是高级接口只支持将下载文件直接写入本地，而不是 stream，会增加一次读写操作

### 3. 结论

先使用 SDK 以使 COS 可以快速投入使用，后续功能细化使用 API 改造

## 现存的问题

1. 使用的是腾讯云单次下载接口，存在下载大小的上限
2. 不支持分批下载
3. 文件夹和文件未作区分，页面浏览时并未分类显示
4. 删除文件时，需要先删除文件，再删除文件夹
5. 对于缓存管理较为草率，需要详细规划
6. 对于页面的刷新也存在部分问题

## 后续制作计划

1. 将文件夹在页面显示中隐藏
2. 调整页面刷新逻辑
3. 将整体逻辑调整为 API 调用