97 lines
3.6 KiB
C#
97 lines
3.6 KiB
C#
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|