diff options
-rw-r--r-- | Timeline/Configs/TencentCosConfig.cs | 13 | ||||
-rw-r--r-- | Timeline/Services/TencentCloudCosService.cs | 94 | ||||
-rw-r--r-- | Timeline/Startup.cs | 3 | ||||
-rw-r--r-- | Timeline/Timeline.csproj | 1 |
4 files changed, 111 insertions, 0 deletions
diff --git a/Timeline/Configs/TencentCosConfig.cs b/Timeline/Configs/TencentCosConfig.cs new file mode 100644 index 00000000..c41669f1 --- /dev/null +++ b/Timeline/Configs/TencentCosConfig.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +namespace Timeline.Configs +{ + public class TencentCosConfig + { + public string AppId { get; set; } + public string Region { get; set; } + public string SecretId { get; set; } + public string SecretKey { get; set; } + } +} diff --git a/Timeline/Services/TencentCloudCosService.cs b/Timeline/Services/TencentCloudCosService.cs new file mode 100644 index 00000000..f1f52ec5 --- /dev/null +++ b/Timeline/Services/TencentCloudCosService.cs @@ -0,0 +1,94 @@ +using COSXML; +using COSXML.Auth; +using COSXML.CosException; +using COSXML.Model; +using COSXML.Model.Object; +using COSXML.Model.Tag; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Threading.Tasks; +using Timeline.Configs; + +namespace Timeline.Services +{ + public interface ITencentCloudCosService + { + Task<bool> Exists(string bucket, string key); + string GetObjectUrl(string bucket, string key); + } + + public class TencentCloudCosService : ITencentCloudCosService + { + private readonly TencentCosConfig _config; + private readonly CosXmlServer _server; + private readonly ILogger<TencentCloudCosService> _logger; + + public TencentCloudCosService(IOptions<TencentCosConfig> config, ILogger<TencentCloudCosService> logger) + { + _config = config.Value; + _logger = logger; + + var cosConfig = new CosXmlConfig.Builder() + .IsHttps(true) + .SetAppid(config.Value.AppId) + .SetRegion(config.Value.Region) + .SetDebugLog(true) + .Build(); + + var credentialProvider = new DefaultQCloudCredentialProvider(config.Value.SecretId, config.Value.SecretKey, 3600); + + _server = new CosXmlServer(cosConfig, credentialProvider); + } + + public Task<bool> Exists(string bucket, string key) + { + bucket = bucket + "-" + _config.AppId; + + var request = new HeadObjectRequest(bucket, key); + + var t = new TaskCompletionSource<bool>(); + + _server.HeadObject(request, delegate (CosResult result) + { + if (result.httpCode >= 200 && result.httpCode < 300) + t.SetResult(true); + else + t.SetResult(false); + }, + delegate (CosClientException clientException, CosServerException serverException) + { + if (clientException != null) + { + _logger.LogError(clientException, "An client error occured when test cos object existence. Bucket : {} . Key : {} .", bucket, key); + t.SetException(clientException); + return; + } + if (serverException != null) + { + _logger.LogError(serverException, "An server error occured when test cos object existence. Bucket : {} . Key : {} .", bucket, key); + t.SetException(serverException); + return; + } + _logger.LogError("An unknown error occured when test cos object existence. Bucket : {} . Key : {} .", bucket, key); + t.SetException(new Exception("Unknown exception when test cos object existence.")); + }); + + return t.Task; + } + + public string GetObjectUrl(string bucket, string key) + { + return _server.GenerateSignURL(new PreSignatureStruct() + { + appid = _config.AppId, + region = _config.Region, + bucket = bucket + "-" + _config.AppId, + key = key, + httpMethod = "GET", + isHttps = true, + signDurationSecond = 300 + }); + } + } +} diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 285dfcfa..6491554a 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -80,6 +80,9 @@ namespace Timeline warnings.Throw(RelationalEventId.QueryClientEvaluationWarning); }); }); + + services.Configure<TencentCosConfig>(Configuration.GetSection(nameof(TencentCosConfig))); + services.AddSingleton<ITencentCloudCosService, TencentCloudCosService>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj index 93513bd3..c9454ec9 100644 --- a/Timeline/Timeline.csproj +++ b/Timeline/Timeline.csproj @@ -12,5 +12,6 @@ <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" /> + <PackageReference Include="Tencent.QCloud.Cos.Sdk.NetCore" Version="5.4.1.1" /> </ItemGroup> </Project> |