NETCORE -MinIO的基本使用
NETCORE -MinIO的基本使用
环境:.net6 + minio3.1.13
minio版本比较老了
minio服务部署:https://www.cnblogs.com/1285026182YUAN/p/18308075
一. 创建 net6项目
二. 安装minio nuget包
三.新建接口
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Minio.DataModel; using Minio; using System.Security.AccessControl; using System.Diagnostics; using System.Net; using Minio.Exceptions; using System.Net.Mime; namespace NETCORE.TMinio.Controllers { [Route("api/[controller]")] [ApiController] public class TextController : ControllerBase { public TextController() { } [HttpPost] [Route("MiniotAsync")] public async Task<IActionResult> MiniotAsync() {
// 注意不带前面的http或https
// 如https的需.WithSSL()的方式
var minioClient = new MinioClient("172.16.50.188:9200", "username", "miyao"); // 现在你可以使用 minioClient 来进行操作,例如列出所有存储桶 try { var buckets = minioClient.ListBucketsAsync().Result; var BucketName = "sn-public"; // 确保存储桶存在 bool found = await minioClient.BucketExistsAsync(BucketName); if (!found) { await minioClient.MakeBucketAsync(BucketName); Console.WriteLine("Bucket {0} created successfully", BucketName); } var FilePath = Directory.GetCurrentDirectory() + "\\tt.txt"; // 读取文件内容并上传 using (var stream = System.IO.File.OpenRead(FilePath)) { await minioClient.PutObjectAsync(BucketName, "tttttttttt", stream, stream.Length); Console.WriteLine("File uploaded successfully"); } } catch (MinioException e) { Console.WriteLine("Error: {0}", e.Message); } return Ok(); } } }
上传完成
参考:https://blog.csdn.net/LoveQHM/article/details/136456884