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;
}
///
/// 上传文件
///
///
///
public async Task 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;
}
///
/// 文件下载链接
///
///
public async Task 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;
}
}
///
/// 单个删除文件
///
///
///
public async Task MinloRemoveFileAsync(string fileName)
{
await _client.RemoveObjectAsync(new RemoveObjectArgs()
.WithBucket(bucketName)
.WithObject(fileName));
}
}
}