下面是获取方法
# NuGet下载
PM>install-Package Minio
using Minio;
using Minio.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public class BucketManager
{
private MinioClient minioClient;
public BucketManager(string minioAddress, string minioAdmin, string minioPassword)
{
this.minioClient = new MinioClient()
.WithEndpoint(minioAddress)
.WithCredentials(minioAdmin, minioPassword)
.WithSSL() // Use this method only if SSL is needed
.Build();
}
public async Task CreateBucketAsync(string bucketName)
{
try
{
bool found = await this.minioClient.BucketExistsAsync(bucketName);
if (found)
{
Console.WriteLine("该存储桶已经存在");
}
else
{
await this.minioClient.MakeBucketAsync(bucketName);
Console.WriteLine("存储桶创建成功");
}
}
catch (MinioException e)
{
Console.WriteLine("Error: " + e.Message);
}
}
public async Task ListBucketsAsync()
{
try
{
List<Bucket> buckets = await this.minioClient.ListBucketsAsync();
foreach (var bucket in buckets)
{
Console.WriteLine(bucket.Name);
}
}
catch (MinioException e)
{
Console.WriteLine("Error: " + e.Message);
}
}
public async Task RemoveBucketAsync(string bucketName)
{
try
{
await this.minioClient.RemoveBucketAsync(bucketName);
Console.WriteLine("删除存储桶 " + bucketName + " 成功");
}
catch (MinioException e)
{
Console.WriteLine("Error: " + e.Message);
}
}
public async Task UploadFileAsync(string bucketName, string objectName, string filePath)
{
try
{
bool found = await this.minioClient.BucketExistsAsync(bucketName);
if (!found)
{
await this.minioClient.MakeBucketAsync(bucketName);
}
await this.minioClient.PutObjectAsync(bucketName, objectName, filePath, contentType: "application/octet-stream");
Console.WriteLine("File uploaded successfully.");
}
catch (MinioException e)
{
Console.WriteLine("Error: " + e.Message);
}
}
public async Task DownloadFileAsync(string bucketName, string objectName, string filePath)
{
try
{
await this.minioClient.GetObjectAsync(bucketName, objectName, filePath);
Console.WriteLine("File downloaded successfully.");
}
catch (MinioException e)
{
Console.WriteLine("Error: " + e.Message);
}
}
public async Task RemoveObjectAsync(string bucketName, string objectName)
{
try
{
await this.minioClient.RemoveObjectAsync(bucketName, objectName);
Console.WriteLine("Object removed successfully.");
}
catch (MinioException e)
{
Console.WriteLine("Error: " + e.Message);
}
}
public bool PathValidation(string objectName)
{
string pattern = @"^a\/(b|c)\/[^\/]+\/[^\/]+