CSharp-Single-EFCore/Bunny.Service/IService/Service/MinioService.cs

54 lines
1.6 KiB
C#
Raw Normal View History

using Bunny.Common.Connect;
using Microsoft.AspNetCore.Http;
using Minio;
using Minio.DataModel.Args;
using Minio.DataModel.Result;
namespace Bunny.Service.IService.Service;
public class MinioService : IMinioService
{
private readonly IMinioClient _minioContext = MinioContext.MinioClient!;
/// <summary>
/// 上传文件
/// </summary>
/// <param name="file">文件内容</param>
public async void SaveMinioFile(IFormFile? file)
{
if (file == null)
{
Console.WriteLine("文件为空");
return;
}
var fileName = file.FileName;
var contentType = file.ContentType;
var putObjectResponse = await _minioContext.PutObjectAsync(new PutObjectArgs()
.WithBucket(MinioContext.BucketName)
.WithObject(fileName)
.WithStreamData(file.OpenReadStream())
.WithContentType(contentType)
.WithObjectSize(file.Length)
);
Console.WriteLine($"putObjectResponse.ObjectNam---{putObjectResponse.ObjectName}");
}
/// <summary>
/// 查询所有的桶
/// </summary>
/// <returns>桶列表</returns>
public ListAllMyBucketsResult GetAllMyBuckets()
{
var listBucketsAsync = _minioContext!.ListBucketsAsync().Result;
foreach (var bucket in listBucketsAsync.Buckets)
{
Console.WriteLine(bucket.Name);
Console.WriteLine(bucket.CreationDate);
Console.WriteLine(bucket.CreationDateDateTime);
}
return listBucketsAsync;
}
}