Files
jinko.yikatong/Src/Dlhis.Common/Services/Implements/MinioService.cs
2024-01-25 15:31:02 +08:00

85 lines
2.5 KiB
C#

using Dlhis.Common.Request;
using Dlhis.Entity.Common;
using Google.Protobuf.WellKnownTypes;
using Kurisu.SqlSugar.Services;
using Minio;
using Minio.DataModel.Args;
using Minio.Exceptions;
using Npgsql.Replication.PgOutput.Messages;
using System.IO;
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));//链接时效
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));
}
}
}