feat: 新增SDK模块、OSS相关功能并优化Redis与代码扩展
1. 新增API版本枚举SDK项,创建SDK基础控制器 2. 添加IList、string扩展方法,新增时间戳扩展工具 3. 新增OSS配置、STS凭证服务、文件处理相关代码 4. 重构Redis缓存组件,替换StackExchange.Redis为CSRedisCore 5. 修复原有Redis操作方法调用,新增基础控制器快捷返回方法 6. 新增阿里云OSS、短信、VOD相关SDK依赖 7. 配置阿里云OSS相关参数到appsettings
This commit is contained in:
96
QYZH.InteractiveMagazine.Infrastructure/OSS/OssIMMService.cs
Normal file
96
QYZH.InteractiveMagazine.Infrastructure/OSS/OssIMMService.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using Aliyun.Credentials.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Web;
|
||||
using Tea;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Infrastructure.OSS
|
||||
{
|
||||
public class OssIMMService
|
||||
{
|
||||
private readonly AlibabaCloud.SDK.Imm20200930.Client _client;
|
||||
private readonly OSSOptions _oSSOptions;
|
||||
private readonly IConfiguration _configuration;
|
||||
public OssIMMService(IConfiguration configuration,IOptionsMonitor<OSSOptions> ossOptions)
|
||||
{
|
||||
this._configuration = configuration;
|
||||
_oSSOptions = ossOptions.CurrentValue;
|
||||
Config config = new Config()
|
||||
{
|
||||
Type = "access_key",
|
||||
AccessKeyId = _oSSOptions.AccessKeyID,
|
||||
AccessKeySecret = _oSSOptions.AccessKeySecret,
|
||||
};
|
||||
|
||||
Aliyun.Credentials.Client credential = new Aliyun.Credentials.Client(config);
|
||||
AlibabaCloud.OpenApiClient.Models.Config config1 = new AlibabaCloud.OpenApiClient.Models.Config
|
||||
{
|
||||
Credential = credential,
|
||||
};
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/imm
|
||||
config1.Endpoint = $"imm.cn-{_oSSOptions.Region}.aliyuncs.com";
|
||||
_client = new AlibabaCloud.SDK.Imm20200930.Client(config1);
|
||||
}
|
||||
|
||||
|
||||
public string DocConvertPng(string source, string prefix)
|
||||
{
|
||||
var createOfficeConversionTaskRequest = new AlibabaCloud.SDK.Imm20200930.Models.CreateOfficeConversionTaskRequest
|
||||
{
|
||||
ProjectName = _oSSOptions.ProjectName,
|
||||
TargetType = "png",
|
||||
SourceURI = $"oss://{_oSSOptions.BucketName}/{HttpUtility.UrlDecode(source)}",
|
||||
TargetURIPrefix = $"oss://{_oSSOptions.BucketName}/{prefix}",
|
||||
//TargetURI = "oss://returnzero/output1/{dirname}/{barename}/{index}.{autoext}",//oss://examplebucket/outputprefix/
|
||||
};
|
||||
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
|
||||
try
|
||||
{
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
var data = _client.CreateOfficeConversionTaskWithOptions(createOfficeConversionTaskRequest, runtime);
|
||||
if (data?.StatusCode == 200)
|
||||
{
|
||||
return data.Body.TaskId;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (Exception _error)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool GetTask(string taskId)
|
||||
{
|
||||
AlibabaCloud.SDK.Imm20200930.Models.GetTaskRequest getTaskRequest = new()
|
||||
{
|
||||
ProjectName = _oSSOptions.ProjectName,
|
||||
TaskType = "OfficeConversion",
|
||||
TaskId = taskId,
|
||||
};
|
||||
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
|
||||
try
|
||||
{
|
||||
var data = _client.GetTaskWithOptions(getTaskRequest, runtime);
|
||||
if (data?.StatusCode == 200)
|
||||
{
|
||||
if (data.Body.Status == "Running") return GetTask(taskId);
|
||||
if (data.Body.Status == "Succeeded") return true;
|
||||
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
catch (Exception _error)
|
||||
{
|
||||
TeaException error = new TeaException(new Dictionary<string, object>
|
||||
{
|
||||
{ "message", _error.Message }
|
||||
});
|
||||
Console.WriteLine(error.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user