feat minlo初步搭建
This commit is contained in:
@@ -2,17 +2,18 @@
|
|||||||
using Dlhis.Common.Response;
|
using Dlhis.Common.Response;
|
||||||
using Dlhis.Common.Services;
|
using Dlhis.Common.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Dlhis.Common.Controllers;
|
namespace Dlhis.Common.Controllers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文件上传/下载
|
/// 文件上传/下载
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class ApkFIileController : ControllerBase
|
public class ApkFIileController : ControllerBase
|
||||||
{
|
{
|
||||||
private IApkFIileService _apkFIileService;
|
private IApkFIileService _apkFIileService;
|
||||||
|
|
||||||
@@ -26,10 +27,24 @@ namespace Dlhis.Common.Controllers;
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task FormSingleFileUpload(ApkRequest request)
|
public async Task FormSingleFileUpload([FromForm] ApkRequest request)
|
||||||
{
|
{
|
||||||
await _apkFIileService.FormSingleFileUpload(request);
|
await _apkFIileService.FormSingleFileUpload(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 文件下载
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("url")]
|
||||||
|
public async Task<string> RequestDownloadFile()
|
||||||
|
{
|
||||||
|
return await _apkFIileService.RequestDownloadFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Kurisu.AspNetCore.Startup;
|
using Kurisu.AspNetCore.Startup;
|
||||||
using Kurisu.SqlSugar.Extensions;
|
using Kurisu.SqlSugar.Extensions;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Minio;
|
using Minio;
|
||||||
|
|
||||||
namespace Dlhis.Common.Packs
|
namespace Dlhis.Common.Packs
|
||||||
@@ -11,10 +12,9 @@ namespace Dlhis.Common.Packs
|
|||||||
public override void ConfigureServices(IServiceCollection services)
|
public override void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
//注入minioClient
|
//注入minioClient
|
||||||
var minioClient = (MinioClient)new MinioClient()
|
IMinioClient minioClient = (MinioClient)new MinioClient()
|
||||||
.WithEndpoint(Configuration["MinIO:Endpoint"])
|
.WithEndpoint("192.168.199.124:9000")
|
||||||
.WithCredentials(Configuration["MinIO:AccessKey"], Configuration["MinIO:SecretKey"])
|
.WithCredentials("Zbjj05OFtR19e5TJopQU", "UMXtNdZhufhAAyYJ3XND4cEgZNUsgOWtbUzksizK")
|
||||||
.WithSSL()
|
|
||||||
.Build();
|
.Build();
|
||||||
services.AddSingleton(minioClient);
|
services.AddSingleton(minioClient);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ namespace Dlhis.Common.Services;
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task FormSingleFileUpload(ApkRequest request);
|
Task FormSingleFileUpload(ApkRequest request);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 文件下载
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<string> RequestDownloadFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
11
Src/Dlhis.Common/Services/IMinioService.cs
Normal file
11
Src/Dlhis.Common/Services/IMinioService.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using Dlhis.Common.Request;
|
||||||
|
|
||||||
|
namespace Dlhis.Common.Services
|
||||||
|
{
|
||||||
|
public interface IMinioService : IScopeDependency
|
||||||
|
{
|
||||||
|
Task MinloUploadFileAsync(ApkRequest request);
|
||||||
|
Task<string> MinloDownloadLinkAsync();
|
||||||
|
Task MinloRemoveFileAsync(ApkRequest request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,22 +10,23 @@ using Minio.DataModel.Args;
|
|||||||
using Minio.Exceptions;
|
using Minio.Exceptions;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using System;
|
using System;
|
||||||
|
using Google.Protobuf.WellKnownTypes;
|
||||||
|
using Minio.DataModel.Encryption;
|
||||||
|
using MySqlX.XDevAPI;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Dlhis.Common.Services.Implements;
|
namespace Dlhis.Common.Services.Implements;
|
||||||
|
|
||||||
public class ApkFIileService : IApkFIileService
|
public class ApkFIileService : IApkFIileService
|
||||||
{
|
{
|
||||||
|
private readonly IMinioService _minioService;
|
||||||
|
|
||||||
private readonly IDbContext _dbContext;
|
public ApkFIileService( IMinioService minioService)
|
||||||
private readonly MinioClient _client;
|
|
||||||
|
|
||||||
public ApkFIileService(IDbContext dbContext, MinioClient client)
|
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_minioService = minioService;
|
||||||
_client = client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文件上传
|
/// 文件上传
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -33,33 +34,16 @@ public class ApkFIileService : IApkFIileService
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task FormSingleFileUpload(ApkRequest request)
|
public async Task FormSingleFileUpload(ApkRequest request)
|
||||||
{
|
{
|
||||||
//上传文件返回地址
|
await _minioService.MinloUploadFileAsync(request);
|
||||||
var url = "https://raw.githubusercontent.com/feicien/android-auto-update/develop/extras/android-auto-update-v1.3.apk";
|
|
||||||
|
|
||||||
var apkInfo = new ApkFIileEntity
|
|
||||||
{
|
|
||||||
Url = url,
|
|
||||||
VersionCode = request.VersionCode,
|
|
||||||
UpdateMessage = request.UpdateMessage,
|
|
||||||
};
|
|
||||||
await _dbContext.InsertAsync(apkInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UploadFile(IFormFile formFile,string bucketName = "test")
|
/// <summary>
|
||||||
|
/// 文件下载
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<string> RequestDownloadFile()
|
||||||
{
|
{
|
||||||
|
return await _minioService.MinloDownloadLinkAsync();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//文件下载
|
|
||||||
[HttpGet]
|
|
||||||
public IActionResult RequestDownloadFile(string filename)
|
|
||||||
{
|
|
||||||
|
|
||||||
var FilePath =" ";
|
|
||||||
|
|
||||||
return new FileStreamResult(new FileStream(FilePath, FileMode.Open), "application/octet-stream") { FileDownloadName = filename };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
107
Src/Dlhis.Common/Services/Implements/MinioService.cs
Normal file
107
Src/Dlhis.Common/Services/Implements/MinioService.cs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 上传文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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<ApkFileEntity>().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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 文件下载链接
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<string> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 单个删除文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task MinloRemoveFileAsync(ApkRequest request)
|
||||||
|
{
|
||||||
|
string fileName = request.formFile.FileName;
|
||||||
|
await _client.RemoveObjectAsync(new RemoveObjectArgs()
|
||||||
|
.WithBucket(bucketName)
|
||||||
|
.WithObject(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,12 +31,12 @@
|
|||||||
//minIO配置
|
//minIO配置
|
||||||
"MinIO": {
|
"MinIO": {
|
||||||
//服务器IP
|
//服务器IP
|
||||||
"Endpoint": "http://192.168.199.124:9001",
|
"Endpoint": "http://192.168.199.124:9000",
|
||||||
//账号
|
//账号
|
||||||
"AccessKey": "Zbjj05OFtR19e5TJopQU",
|
"AccessKey": "Zbjj05OFtR19e5TJopQU",
|
||||||
//密码
|
//密码
|
||||||
"SecretKey": "UMXtNdZhufhAAyYJ3XND4cEgZNUsgOWtbUzksizK",
|
"SecretKey": "UMXtNdZhufhAAyYJ3XND4cEgZNUsgOWtbUzksizK",
|
||||||
//默认存储桶
|
//默认存储桶
|
||||||
"Bucket": "tests"
|
"Bucket": "apk"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,20 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Dlhis.Entity.Common
|
namespace Dlhis.Entity.Common;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Apk文件表
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable(TableName = "ApkFIile", TableDescription = "Apk文件表")]
|
||||||
|
public class ApkFileEntity : SugarBaseEntity
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Apk文件表
|
/// 文件名称
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SugarTable(TableName = "ApkFIile", TableDescription = "Apk文件表")]
|
[SugarColumn(ColumnDescription = "文件名称")]
|
||||||
public class ApkFIileEntity : SugarBaseEntity
|
public string Name { get; set; }
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 地址
|
/// 地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -32,4 +38,5 @@ namespace Dlhis.Entity.Common
|
|||||||
[SugarColumn(ColumnDescription = "修改信息")]
|
[SugarColumn(ColumnDescription = "修改信息")]
|
||||||
public string UpdateMessage { get; set; }
|
public string UpdateMessage { get; set; }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user