Files
jinko.yikatong/Src/Dlhis.Common/Services/Implements/MinioService.cs
2024-01-26 10:26:06 +08:00

80 lines
2.3 KiB
C#

using Dlhis.Common.Request;
using Minio;
using Minio.DataModel.Args;
using Minio.Exceptions;
namespace Dlhis.Common.Services.Implements
{
public class MinioService : IMinioService
{
private readonly IMinioClient _client;
private static string bucketName = "apk";
public MinioService( IMinioClient client)
{
_client = client;
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<string> MinloUploadFileAsync(ApkRequest request)
{
string fileName = request.formFile.FileName;
using (MemoryStream filestream = new MemoryStream())
{
await request.formFile.CopyToAsync(filestream);
filestream.Position = 0;
var res = await _client.PutObjectAsync(new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(fileName)
.WithStreamData(filestream)
.WithObjectSize(filestream.Length));
}
var filerl = await MinloDownloadLinkAsync(fileName);
return filerl;
}
/// <summary>
/// 文件下载链接
/// </summary>
/// <returns></returns>
public async Task<string> MinloDownloadLinkAsync(string fileName)
{
try
{
var url = await _client.PresignedGetObjectAsync(new PresignedGetObjectArgs()
.WithBucket(bucketName)
.WithObject(fileName)
.WithExpiry(60 * 60 * 24*7));//链接时效
return url;
}
catch (MinioException e)
{
Console.Out.WriteLine("Error occurred: " + e);
return null;
}
}
/// <summary>
/// 单个删除文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public async Task MinloRemoveFileAsync(string fileName)
{
await _client.RemoveObjectAsync(new RemoveObjectArgs()
.WithBucket(bucketName)
.WithObject(fileName));
}
}
}