From 45c09ace4acee4df9c860395fb7261adb09bc914 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 22 Apr 2019 17:03:07 +0800 Subject: Add Tencent COS. --- Timeline/Configs/TencentCosConfig.cs | 13 ++++ Timeline/Services/TencentCloudCosService.cs | 94 +++++++++++++++++++++++++++++ Timeline/Startup.cs | 3 + Timeline/Timeline.csproj | 1 + 4 files changed, 111 insertions(+) create mode 100644 Timeline/Configs/TencentCosConfig.cs create mode 100644 Timeline/Services/TencentCloudCosService.cs 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 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 _logger; + + public TencentCloudCosService(IOptions config, ILogger 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 Exists(string bucket, string key) + { + bucket = bucket + "-" + _config.AppId; + + var request = new HeadObjectRequest(bucket, key); + + var t = new TaskCompletionSource(); + + _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(Configuration.GetSection(nameof(TencentCosConfig))); + services.AddSingleton(); } // 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 @@ + -- cgit v1.2.3