Bitzsoft.Integrations.FileStorage.Tencent
1.0.0-alpha.7
dotnet add package Bitzsoft.Integrations.FileStorage.Tencent --version 1.0.0-alpha.7
NuGet\Install-Package Bitzsoft.Integrations.FileStorage.Tencent -Version 1.0.0-alpha.7
<PackageReference Include="Bitzsoft.Integrations.FileStorage.Tencent" Version="1.0.0-alpha.7" />
<PackageVersion Include="Bitzsoft.Integrations.FileStorage.Tencent" Version="1.0.0-alpha.7" />
<PackageReference Include="Bitzsoft.Integrations.FileStorage.Tencent" />
paket add Bitzsoft.Integrations.FileStorage.Tencent --version 1.0.0-alpha.7
#r "nuget: Bitzsoft.Integrations.FileStorage.Tencent, 1.0.0-alpha.7"
#:package Bitzsoft.Integrations.FileStorage.Tencent@1.0.0-alpha.7
#addin nuget:?package=Bitzsoft.Integrations.FileStorage.Tencent&version=1.0.0-alpha.7&prerelease
#tool nuget:?package=Bitzsoft.Integrations.FileStorage.Tencent&version=1.0.0-alpha.7&prerelease
Bitzsoft.Integrations.FileStorage.Tencent
腾讯云 COS 文件存储实现。
功能特性
- 完整实现
IFileStore接口:覆盖所有文件与存储桶操作 - 扩展接口
ITencentCosFileStore:腾讯云 COS 文件存储服务标识接口,支持未来扩展 - 预签名 URL:生成带过期时间的下载地址和上传地址
- 存储桶管理:创建、删除、判断存储桶是否存在
- 文件元数据:保存文件时支持设置 ContentType
- 配置提供器模式:通过
ITencentCosConfigProvider抽象配置获取,支持静态配置或动态配置中心 - IHttpClientFactory 集成:通过
Microsoft.Extensions.Http管理 HTTP 连接
安装
.NET CLI
dotnet add package Bitzsoft.Integrations.FileStorage.Tencent
PackageReference
<PackageReference Include="Bitzsoft.Integrations.FileStorage.Tencent" Version="1.0.0" />
配置
appsettings.json
{
"TencentCos": {
"AppId": "1234567890",
"SecretId": "your-secret-id",
"SecretKey": "your-secret-key",
"Region": "ap-guangzhou",
"DefaultBucketName": "my-app-bucket",
"HttpClientName": "FileStorage",
"UploadUrlExpiration": 3600,
"DownloadUrlExpiration": 43200
}
}
| 配置项 | 说明 | 默认值 |
|---|---|---|
AppId |
腾讯云 AppId | -- |
SecretId |
腾讯云 SecretId | -- |
SecretKey |
腾讯云 SecretKey | -- |
Region |
COS 区域,例如 ap-guangzhou | -- |
DefaultBucketName |
默认存储桶名称,不含 AppId 后缀,SDK 内部自动拼接 | -- |
HttpClientName |
HttpClient 名称,用于 IHttpClientFactory 创建客户端 |
-- |
UploadUrlExpiration |
上传预签名地址过期时间(秒) | 3600 |
DownloadUrlExpiration |
下载预签名地址过期时间(秒) | 43200 |
注册服务
使用 DI 扩展方法(推荐)
using Bitzsoft.Integrations.FileStorage.Tencent;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
// 注册 HttpClient 工厂
services.AddHttpClient("FileStorage");
// 一行注册腾讯云 COS 文件存储服务
services.AddBitzsoftTencentCosFileStorage(options =>
{
options.AppId = "1234567890";
options.SecretId = "your-secret-id";
options.SecretKey = "your-secret-key";
options.Region = "ap-guangzhou";
options.DefaultBucketName = "my-app-bucket";
});
从 IConfiguration 绑定配置
using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Tencent;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var services = new ServiceCollection();
// 注册 HttpClient 工厂(TencentCosFileStore 通过 IHttpClientFactory 创建 HTTP 客户端)
services.AddHttpClient("FileStorage");
// 注册处理器工厂
services.AddSingleton<IFileNameProcessorFactory, FileNameProcessorFactory>();
services.AddSingleton<IBucketNameProcessorFactory, BucketNameProcessorFactory>();
// 从配置节绑定 TencentCosOptions
services.AddSingleton<ITencentCosConfigProvider>(sp =>
{
var options = configuration.GetSection("TencentCos").Get<TencentCosOptions>();
return new TencentCosConfigProvider(options);
});
services.AddSingleton<ITencentCosFileStore, TencentCosFileStore>();
services.AddSingleton<IFileStore>(sp => sp.GetRequiredService<ITencentCosFileStore>());
使用 IOptions 模式
using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Tencent;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddHttpClient("FileStorage");
services.AddSingleton<IFileNameProcessorFactory, FileNameProcessorFactory>();
services.AddSingleton<IBucketNameProcessorFactory, BucketNameProcessorFactory>();
// 配置选项
services.Configure<TencentCosOptions>(
configuration.GetSection("TencentCos"));
// TencentCosConfigProvider 内部通过 IOptions<TencentCosOptions> 获取配置
services.AddSingleton<ITencentCosConfigProvider, TencentCosConfigProvider>();
services.AddSingleton<ITencentCosFileStore, TencentCosFileStore>();
services.AddSingleton<IFileStore>(sp => sp.GetRequiredService<ITencentCosFileStore>());
第三方请求日志
本连接器基于腾讯云 COS SDK,HTTP 管道封装在 SDK 内部,无法用 DelegatingHandler 拦截(与 HttpClient 直连实现不同)。请求日志改由 IRequestLogRecorder 回调钩子实现,在对象存储操作包装层记录每次 API 调用,汇入与 HttpClient 拦截同一个日志管道(IRequestLogStore),由宿主统一持久化。
每个对象存储操作产生一条日志记录:
| 字段 | 取值 |
|---|---|
Provider |
FileStorage.Tencent |
Endpoint |
COS 服务域名(如 cos.ap-guangzhou.myqcloud.com) |
Method |
SDK(非标准 HTTP 方法,标识为 SDK 管道调用) |
ApiName |
操作名,如 Upload / Download / List / Delete / Exists 等 |
RequestBody / ResponseBody |
请求/响应摘要序列化(经 SensitiveFields 脱敏) |
StatusCode |
成功记 200;SDK 抛异常时为空 |
Exception |
SDK 异常信息 |
DurationMs |
本次操作耗时 |
由于 SDK 不暴露原始 HTTP 状态码与响应头,StatusCode 无法像 HttpClient 拦截那样记录真实值(成功记 200、异常时留空),也无法记录 RequestHeaders。此外,仅本连接器已实现的对象存储操作会被记录,SDK 的其他能力(如生命周期、跨域、权限策略等)不在记录范围。
// ① 默认:启用回调但不持久化(日志丢弃)
services.AddBitzsoftTencentCosFileStorage(options => { /* ... */ });
// ② 持久化:宿主注册 IRequestLogStore 实现后,对象存储操作自动落库
// AddBitzsoftTencentCosFileStorage 内部已注入 IRequestLogRecorder,宿主无需再额外挂载 handler
services.AddRequestLogging<MyRequestLogStore>(opts =>
{
opts.MaxBodyLength = 0; // 下载响应可能很长,0=不截断完整保留
opts.SensitiveFields.Add("mySecret"); // 额外脱敏字段
});
services.AddBitzsoftTencentCosFileStorage(options => { /* ... */ });
自定义
IRequestLogStore实现(SqlSugar / Dapper / MongoDB)详见Bitzsoft.Integrations.RequestLogging。
使用示例
保存文件并获取下载链接
using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Tencent;
public class ReportService
{
private readonly ITencentCosFileStore _fileStore;
public ReportService(ITencentCosFileStore fileStore)
{
_fileStore = fileStore;
}
public async Task<string> UploadReportAsync(Stream pdfStream, string reportName)
{
// 保存 PDF 到默认存储桶
var result = await _fileStore.SaveFileAsync(
pdfStream, $"{reportName}.pdf");
// 生成 12 小时有效的下载链接
var downloadUrl = await _fileStore.GenerateDownloadUrlAsync(
result.FilePath);
return downloadUrl;
}
}
通过 IFileStore 抽象使用
using Bitzsoft.Integrations.FileStorage;
public class ContractService
{
private readonly IFileStore _fileStore;
public ContractService(IFileStore fileStore)
{
_fileStore = fileStore;
}
public async Task<FileResult> UploadContractAsync(
Stream contractStream, string contractId)
{
var args = new SaveFileArgs($"{contractId}.pdf", contractStream)
{
ContentType = "application/pdf"
};
return await _fileStore.SaveFileAsync(args);
}
}
从远程 URL 拉取文件到 COS
using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Tencent;
public class MediaSyncService
{
private readonly IFileStore _fileStore;
public MediaSyncService(IFileStore fileStore)
{
_fileStore = fileStore;
}
public async Task<FileResult> SyncExternalImageAsync(string imageUrl, string saveAs)
{
var args = new SaveFileByUrlArgs(saveAs, imageUrl)
{
BucketName = "media-assets"
};
return await _fileStore.SaveFileByUrlAsync(args);
}
}
复制与移动文件
using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Tencent;
public class FileOrganizer
{
private readonly IFileStore _fileStore;
public FileOrganizer(IFileStore fileStore)
{
_fileStore = fileStore;
}
public async Task ArchiveFileAsync(string sourceFile, string archivePath)
{
// 复制文件到归档路径
await _fileStore.CopyFileAsync(sourceFile, archivePath);
// 删除原文件(等同于 MoveFileAsync)
await _fileStore.DeleteFileAsync(sourceFile);
}
public async Task MoveToBucketAsync(
string fileName, string sourceBucket, string destBucket)
{
var sourceArgs = new FileStorageArgs(fileName) { BucketName = sourceBucket };
var destArgs = new FileStorageArgs(fileName) { BucketName = destBucket };
await _fileStore.MoveFileAsync(sourceArgs, destArgs);
}
}
核心类型一览
| 类型 | 说明 |
|---|---|
ITencentCosFileStore |
腾讯云 COS 文件存储接口,继承 IFileStore |
TencentCosFileStore |
默认实现类 |
TencentCosOptions |
腾讯云 COS 连接配置 |
ITencentCosConfigProvider |
配置提供器接口 |
TencentCosConfigProvider |
默认配置提供器(支持 IOptions<TencentCosOptions>) |
ServiceCollectionExtensions |
DI 扩展方法 AddBitzsoftTencentCosFileStorage |
依赖
Bitzsoft.Integrations.Compatibility,基础工具库- Bitzsoft.Integrations.FileStorage,多云文件存储抽象层
Tencent.QCloud.Cos.Sdk,腾讯云 COS 官方 SDKMicrosoft.Extensions.Http,HttpClient 工厂支持
相关包
- Bitzsoft.Integrations.FileStorage,多云文件存储抽象层
- Bitzsoft.Integrations.FileStorage.Minio,MinIO 文件存储实现
- Bitzsoft.Integrations.FileStorage.Aliyun,阿里云 OSS 文件存储实现
- Bitzsoft.Integrations.FileStorage.Azure,Azure Blob Storage 文件存储实现
- Bitzsoft.Integrations.FileStorage.Aws,AWS S3 文件存储实现
- Bitzsoft.Integrations.FileStorage.Huawei,华为云 OBS 文件存储实现
- Bitzsoft.Integrations.FileStorage.Kingsoft,金山云 KS3 文件存储实现
- Bitzsoft.Integrations.FileStorage.Qiniu,七牛云 Kodo 文件存储实现
- Bitzsoft.Integrations.FileStorage.Upyun,又拍云文件存储实现
- Bitzsoft.Integrations.FileStorage.Nas,NAS / 本地文件系统存储实现
- Bitzsoft.Integrations.FileStorage.All,全部云存储实现聚合包
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Bitzsoft.Integrations.Compatibility (>= 1.0.0-alpha.7)
- Bitzsoft.Integrations.FileStorage (>= 1.0.0-alpha.7)
- Bitzsoft.Integrations.RequestLogging (>= 1.0.0-alpha.7)
- Microsoft.Extensions.Http (>= 10.0.9)
- Microsoft.Extensions.Options (>= 10.0.9)
- Newtonsoft.Json (>= 13.0.4)
- Tencent.QCloud.Cos.Sdk (>= 5.4.51)
-
net5.0
- Bitzsoft.Integrations.Compatibility (>= 1.0.0-alpha.7)
- Bitzsoft.Integrations.FileStorage (>= 1.0.0-alpha.7)
- Bitzsoft.Integrations.RequestLogging (>= 1.0.0-alpha.7)
- Microsoft.Extensions.Http (>= 5.0.0)
- Microsoft.Extensions.Options (>= 5.0.0)
- Newtonsoft.Json (>= 13.0.4)
- Tencent.QCloud.Cos.Sdk (>= 5.4.51)
-
net8.0
- Bitzsoft.Integrations.Compatibility (>= 1.0.0-alpha.7)
- Bitzsoft.Integrations.FileStorage (>= 1.0.0-alpha.7)
- Bitzsoft.Integrations.RequestLogging (>= 1.0.0-alpha.7)
- Microsoft.Extensions.Http (>= 10.0.9)
- Microsoft.Extensions.Options (>= 10.0.9)
- Newtonsoft.Json (>= 13.0.4)
- Tencent.QCloud.Cos.Sdk (>= 5.4.51)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Bitzsoft.Integrations.FileStorage.Tencent:
| Package | Downloads |
|---|---|
|
Bitzsoft.Integrations.All
Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块 |
|
|
Bitzsoft.Integrations.FileStorage.All
多云文件存储聚合包 — 包含 Aliyun / Azure / AWS / MinIO / 腾讯云 / 华为云 / 七牛云 / 又拍云 / 金山云 / NAS 全部实现 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-alpha.7 | 58 | 6/16/2026 |
| 1.0.0-alpha.6 | 67 | 6/16/2026 |
| 1.0.0-alpha.5 | 59 | 6/14/2026 |
| 1.0.0-alpha.3 | 65 | 6/7/2026 |