MinIO客户端操作(C#, 上传下载)

可实现文件上传、下载,查询bucket内的所有文件(递归,不返回文件夹),查询某一文件夹或bucket根目录下的文件和文件夹(非递归)。

Bucket类似根文件夹,Object为bucket下实际存储对象(文件或文件夹)。

实例化

new MinioClient()
                    .WithEndpoint("localhost:9200")
                    .WithCredentials("8VqBZTKDTuAO42yb", "HhT5jGEUaqwqzoG7Qc76ndVVbADBwFij")
                    .Build()

上传

await instance.PutObjectAsync(new PutObjectArgs()
			.WithBucket(bucket)
			.WithObject("pic/ubuntu.png")
			.WithFileName("E:\\ubuntu.png")
			.WithContentType("application/octet-stream"));

下载(通过浏览器)

public async Task Download(string bucketName, string path)
        {
            MemoryStream memoryStream = new MemoryStream();
            MinioClient minio = //instance;
            await minio.GetObjectAsync(new GetObjectArgs().WithBucket(bucketName)
                .WithObject(path)
                .WithCallbackStream(stream => {
                    stream.CopyTo(memoryStream);
                }));
            memoryStream.Position = 0;
            return File(memoryStream, "application/octet-stream",path.Split('/').Last());
        }

查询

/// 
        /// 
        /// 
        /// 
        /// 
        /// 文件夹完整路径(带/)
        /// 是否递归
        /// 
        /// 
        public static async Task> GetBucketObjects(MinioClient minio,
        string bucketName,
        string prefix = null,
        bool recursive = false,
        bool versions = false)
        {
            List items = new List();
            try
            {
                var listArgs = new ListObjectsArgs()
                    .WithBucket(bucketName)
                    .WithPrefix(prefix)
                    .WithRecursive(recursive);
                var observable = minio.ListObjectsAsync(listArgs);
                //var subscription = observable.Subscribe(
                //    item => items.Add(item));
                await observable.ForEachAsync(item =>
                {
                    items.Add(item);
                });
            }
            catch (Exception e)
            {
                Console.WriteLine($"[Bucket]  Exception: {e}");
            }

            return items;
        }

你可能感兴趣的:(Ubuntu,MinIO实践,服务器,c#)