aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/TencentCloudCosService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-22 17:03:07 +0800
committercrupest <crupest@outlook.com>2019-04-22 17:03:07 +0800
commit6a73b71e4af6aa30cf6fca301d954bc01927a8c9 (patch)
treec5db45281fd18bd113d1652cf104867db276cb1d /Timeline/Services/TencentCloudCosService.cs
parent58edbb6661c8f7d147f438716b286aa84c6bcb14 (diff)
downloadtimeline-6a73b71e4af6aa30cf6fca301d954bc01927a8c9.tar.gz
timeline-6a73b71e4af6aa30cf6fca301d954bc01927a8c9.tar.bz2
timeline-6a73b71e4af6aa30cf6fca301d954bc01927a8c9.zip
Add Tencent COS.
Diffstat (limited to 'Timeline/Services/TencentCloudCosService.cs')
-rw-r--r--Timeline/Services/TencentCloudCosService.cs94
1 files changed, 94 insertions, 0 deletions
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
+ });
+ }
+ }
+}