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 IDbContext _dbContext; private readonly IMinioClient _client; private static string bucketName = "apk"; public MinioService(IDbContext dbContext, IMinioClient client) { _dbContext = dbContext; _client = client; } /// /// 上传文件 /// /// /// public async Task MinloUploadFileAsync(ApkRequest request) { using (MemoryStream filestream = new MemoryStream()) { await request.formFile.CopyToAsync(filestream); filestream.Position = 0; string fileName = request.formFile.FileName; var res = await _client.PutObjectAsync(new PutObjectArgs() .WithBucket(bucketName) .WithObject(fileName) .WithStreamData(filestream) .WithObjectSize(filestream.Length)); //"\"698d51a19d8a121ce581499d7b701668\"" //更新数据库 var entity = await _dbContext.Queryable().Where(x => x.Name == fileName).FirstAsync(); if (entity == null) { var info = new ApkFileEntity { Name = fileName, Url = fileName, VersionCode = request.VersionCode, UpdateMessage = request.UpdateMessage, }; await _dbContext.InsertAsync(info); } else { entity.Url = fileName; entity.VersionCode = request.VersionCode; entity.UpdateMessage = request.UpdateMessage; } } } /// /// 文件下载链接 /// /// public async Task MinloDownloadLinkAsync() { try { var url = await _client.PresignedGetObjectAsync(new PresignedGetObjectArgs() .WithBucket(bucketName) .WithObject("消费API逻辑.txt") .WithExpiry(60 * 60 * 24));//链接时效 return url; } catch (MinioException e) { Console.Out.WriteLine("Error occurred: " + e); return null; } } /// /// 单个删除文件 /// /// /// public async Task MinloRemoveFileAsync(ApkRequest request) { string fileName = request.formFile.FileName; await _client.RemoveObjectAsync(new RemoveObjectArgs() .WithBucket(bucketName) .WithObject(fileName)); } } }