2024-09-02 09:00:50 +08:00
|
|
|
|
using Bunny.Dao.Model.Result;
|
2024-08-19 22:43:35 +08:00
|
|
|
|
using Bunny.Service.IService;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-08-20 10:14:15 +08:00
|
|
|
|
using Minio.DataModel;
|
2024-08-19 22:43:35 +08:00
|
|
|
|
using Minio.DataModel.Result;
|
|
|
|
|
|
|
|
|
|
namespace Bunny.WebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
[Route("/api/[controller]/[action]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class MinioController(IMinioService minioService) : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有的桶
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public Result<ListAllMyBucketsResult> GetAllMyBuckets()
|
|
|
|
|
{
|
|
|
|
|
var listAllMyBucketsResult = minioService.GetAllMyBuckets();
|
|
|
|
|
return Result<ListAllMyBucketsResult>.Success(listAllMyBucketsResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 上传文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">文件</param>
|
2024-08-20 10:14:15 +08:00
|
|
|
|
/// <param name="filepath"></param>
|
2024-08-19 22:43:35 +08:00
|
|
|
|
[HttpPost]
|
2024-08-20 10:14:15 +08:00
|
|
|
|
public Result<string> SaveMinioFile(IFormFile? file, string filepath = "test")
|
2024-08-19 22:43:35 +08:00
|
|
|
|
{
|
2024-08-20 10:14:15 +08:00
|
|
|
|
minioService.SaveMinioFile(file, filepath);
|
2024-08-19 22:43:35 +08:00
|
|
|
|
return Result<string>.Success("上传成功");
|
|
|
|
|
}
|
2024-08-20 10:14:15 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取文件信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">文件名</param>
|
|
|
|
|
/// <param name="bucketName">桶名称</param>
|
|
|
|
|
/// <returns>ObjectStat</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public Result<ObjectStat> GetObjectStat(string filename, string bucketName = "csharp-test")
|
|
|
|
|
{
|
|
|
|
|
var vo = minioService.GetObjectStat(filename, bucketName);
|
|
|
|
|
return Result<ObjectStat>.Success(vo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取文件路径
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">文件名</param>
|
|
|
|
|
/// <param name="bucketName">桶名称</param>
|
|
|
|
|
/// <returns>文件地址</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public Result<string> GetObjectPath(string filename, string bucketName = "csharp-test")
|
|
|
|
|
{
|
|
|
|
|
var vo = minioService.GetObjectPath(filename, bucketName);
|
|
|
|
|
return Result<string>.Success(vo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 下载文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">文件名</param>
|
|
|
|
|
/// <param name="bucketName">桶名称</param>
|
|
|
|
|
/// <returns>下载文件</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult DownloadObject(string filename, string bucketName = "csharp-test")
|
|
|
|
|
{
|
|
|
|
|
var buffer = minioService.DownloadObject(filename, bucketName).Result;
|
|
|
|
|
return File(buffer, "application/octet-stream", filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查看对象文件展示在网页中
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">文件名</param>
|
|
|
|
|
/// <param name="bucketName">桶名称</param>
|
|
|
|
|
/// <returns>下载文件</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult ViewObject(string filename, string bucketName = "csharp-test")
|
|
|
|
|
{
|
|
|
|
|
var buffer = minioService.DownloadObject(filename, bucketName).Result;
|
|
|
|
|
return File(buffer, "image/jpeg");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 列出文件夹下所有文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filepath">文件夹</param>
|
|
|
|
|
/// <param name="bucketName">桶名称</param>
|
|
|
|
|
/// <returns>所有文件</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public Result<IAsyncEnumerable<Item>> ListObject(string? filepath, string bucketName = "csharp-test")
|
|
|
|
|
{
|
|
|
|
|
var vo = minioService.ListObject(filepath, bucketName);
|
|
|
|
|
return Result<IAsyncEnumerable<Item>>.Success(vo);
|
|
|
|
|
}
|
2024-08-19 22:43:35 +08:00
|
|
|
|
}
|