Minio新版API 有很多改动 网上搜索不到 做个记录。
- 创建Client
- 创建client的方式和以前差别不大
var minioClient = new MinioClient() .WithEndpoint(AppSettings._minio?.EndPoint, (int)(AppSettings._minio?.Port)) .WithCredentials(AppSettings._minio?.AccessKey, AppSettings._minio?.SecretKey) .Build();
- 创建client的方式和以前差别不大
- 创建桶并更改桶文件的过期时间
- 设置Lifecycle
// 创建 bucket await minioClient.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName)); var rule = new LifecycleRule() { Expiration = new Expiration() { Days = 3 } }; var args = new SetBucketLifecycleArgs(); args.WithBucket(bucketName); args.WithLifecycleConfiguration(new LifecycleConfiguration() { Rules = new Collection<LifecycleRule> { rule } }); await minioClient.SetBucketLifecycleAsync(args);
- 设置Lifecycle
- 上传文件
- 字节数组 byte[] byteArray
using (MemoryStream stream = new MemoryStream(byteArray)) { stream.Position = 0; // Upload a file to bucket. await minioClient.PutObjectAsync(new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithObjectSize(dwSize) .WithStreamData(stream) .WithContentType("image/jpeg")).ConfigureAwait(false); }
- 字节数组 byte[] byteArray
- 下载文件
- 流式下载文件
public static async Task<byte[]> DownLoadObjectAsync(string bucketName, string objectName) { MemoryStream resultStream = new MemoryStream(); var getArgs = new GetObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithCallbackStream(async (stream, cancellationToken) => { await stream.CopyToAsync(resultStream, cancellationToken).ConfigureAwait(false); await stream.DisposeAsync().ConfigureAwait(false); }); await minioClient.GetObjectAsync(getArgs); var resultByte = resultStream.GetBuffer(); return resultByte; }
- 流式下载文件
标签:Minio,stream,await,API,bucketName,var,new,NET,minioClient From: https://www.cnblogs.com/xzion/p/18358229