适用于与Amazon S3兼容的云存储的MinIO .NET SDK Slack Build status

MinIO .NET Client SDK提供了简单的API来访问MinIO以及任何与Amazon S3兼容的对象存储服务。有关API和示例的完整列表,请查看Dotnet Client API Reference文档。本文假设你已经有VisualStudio开发环境。

最低需求

使用NuGet安装

为了安装.NET Framework的MinIO .NET包,你可以在Nuget Package Manager控制台运行下面的命令。

PM> Install-Package Minio

MinIO Client示例

MinIO client需要以下4个参数来连接与Amazon S3兼容的对象存储服务。

参数 描述
endpoint 对象存储服务的URL
accessKey Access key是唯一标识你的账户的用户ID。
secretKey Secret key是你账户的密码。
secure true代表使用HTTPS。

下面示例中使用运行在 https://play.min.io:9000 上的MinIO服务,你可以用这个服务来开发和测试。示例中的访问凭据是公开的。

using Minio;

// Initialize the client with access credentials.
private static MinioClient minio = new MinioClient("play.min.io:9000",
                "Q3AM3UQ867SPQQA43P2F",
                "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
                ).WithSSL();

// Create an async task for listing buckets.
var getListBucketsTask = minio.ListBucketsAsync();

// Iterate over the list of buckets.
foreach (Bucket bucket in getListBucketsTask.Result.Buckets)
{
    Console.Out.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime);
}

完整的File Uploader示例

本示例程序连接到一个对象存储服务,创建一个存储桶,并且上传一个文件到该存储桶中。为了运行下面的示例,请点击[Link]启动该项目。

using System;
using Minio;
using Minio.Exceptions;
using Minio.DataModel;
using System.Threading.Tasks;

namespace FileUploader
{
    class FileUpload
    {
        static void Main(string[] args)
        {
            var endpoint  = "play.min.io:9000";
            var accessKey = "Q3AM3UQ867SPQQA43P2F";
            var secretKey = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG";
            try
            {
                var minio = new MinioClient(endpoint, accessKey, secretKey).WithSSL();
                FileUpload.Run(minio).Wait();
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        // File uploader task.
        private async static Task Run(MinioClient minio)
        {
            var bucketName = "mymusic";
            var location   = "us-east-1";
            var objectName = "golden-oldies.zip";
            var filePath = "C:\\Users\\username\\Downloads\\golden_oldies.mp3";
            var contentType = "application/zip";

            try
            {
                // Make a bucket on the server, if not already present.
                bool found = await minio.BucketExistsAsync(bucketName);
                if (!found)
                {
                    await minio.MakeBucketAsync(bucketName, location);
                }
                // Upload a file to bucket.
                await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);
                Console.Out.WriteLine("Successfully uploaded " + objectName );
            }
            catch (MinioException e)
            {
                Console.WriteLine("File Upload Error: {0}", e.Message);
            }
        }
    }
}

运行MinIO Client示例

Windows

$ git clone https://github.com/minio/minio-dotnet && cd minio-dotnet
  //Cases.MakeBucket.Run(minioClient, bucketName).Wait();
在Linux上设置Mono和.NETCore
注意:minio-dotnet需要mono 5.0.1稳定版本和.NET Core 2.0 SDK。
$ ./mono_install.sh
运行Minio.Examples
$ cd Minio.Examples
$ dotnet build -c Release 
$ dotnet run

操作存储桶

存储桶策略

存储桶通知

操作文件对象

操作对象

Presigned操作

客户端自定义设置

了解更多