0%

C# Minio存储文件

MinIO Client SDK 为 MinIO 和 Amazon S3 兼容云存储服务提供更高级别的 API 支持.Net4.5及以上

官方资料

源码: https://github.com/minio/minio-dotnet

Server安装使用

下载Minio

https://dl.minio.io/server/minio/release/windows-amd64/minio.exe

启动服务

启动服务需要用cmd命令行启动
minio.exe server D:\MinIO
后面为minio.exe所在路径
启动成功后可以通过 127.0.0.1:9000 来访问,默认用户名和密码为 minioadmin/minioadmin

修改密码

找到 .minio.sys/config/config.json 文件,打开搜索 access_key
access_key :value 用户名
secret_key :value 密码
保存重启即可

Client安装使用

安装MinIO.NET

在Nuget程序包管理器控制台中运行以下命令
PM> Install-Package Minio

示例: https://github.com/minio/minio-dotnet/tree/master/Minio.Examples/Cases

文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Minio;
using Minio.Exceptions;
using Minio.DataModel;
public static void UploadFile()
{
var endpoint = "127.0.0.1:9000";
var accessKey = "minioadmin";
var secretKey = "minioadmin";

var minio = new MinioClient()
.WithEndpoint(endpoint)
.WithCredentials(accessKey, secretKey)
.Build();
Run(minio).Wait();
}
private async static Task Run(MinioClient minio)
{
var bucketName = "firstbucket";
var location = "us-east-1";
var objectName = "1.bmp";
var filePath = "D:\\BBK\\1.bmp";
var contentType = "application/bmp";

try
{
// Make a bucket on the server, if not already present.
var beArgs = new BucketExistsArgs()
.WithBucket(bucketName);
bool found = await minio.BucketExistsAsync(beArgs).ConfigureAwait(false);
if (!found)
{
var mbArgs = new MakeBucketArgs()
.WithBucket(bucketName);
await minio.MakeBucketAsync(mbArgs).ConfigureAwait(false);
}
// Upload a file to bucket.
var putObjectArgs = new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(objectName)
.WithFileName(filePath)
.WithContentType(contentType);
await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
Console.WriteLine("Successfully uploaded " + objectName);
}
catch (MinioException e)
{
Console.WriteLine("File Upload Error: {0}", e.Message);
}
}

文件下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//未测试
public static async Task Run(IMinioClient minio,
string bucketName = "my-bucket-name",
string objectName = "my-object-name",
string fileName = "local-filename",
ServerSideEncryption sse = null)
{
try
{
Console.WriteLine("Running example for API: GetObjectAsync");
File.Delete(fileName);
var args = new GetObjectArgs()
.WithBucket(bucketName)
.WithObject(objectName)
.WithFile(fileName)
.WithServerSideEncryption(sse);
await minio.GetObjectAsync(args).ConfigureAwait(false);
Console.WriteLine($"Downloaded the file {fileName} from bucket {bucketName}");
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine($"[Bucket] Exception: {e}");
}
}