文档

.NET 快速入门指南

MinIO 客户端 SDK for .NET

MinIO 客户端 SDK 为 MinIO 和与 Amazon S3 兼容的云存储服务提供更高级别的 API。有关 API 和示例的完整列表,请查看 Dotnet 客户端 API 参考。本文档假设您有一个可用的 VisualStudio 开发环境。

Slack Github Actions Nuget GitHub tag (with filter)

从 NuGet 安装

要安装 MinIO .NET 包,请在 Nuget 包管理器控制台中运行以下命令。

PM> Install-Package Minio

MinIO 客户端示例 for ASP.NET

当使用 AddMinio 将 Minio 添加到您的 ServiceCollection 时,Minio 还会使用您添加的任何自定义日志记录提供者,例如 Serilog,在启用时输出跟踪。

using Minio;
using Minio.DataModel.Args;

public static class Program
{
    var endpoint = "play.min.io";
    var accessKey = "minioadmin";
    var secretKey = "minioadmin";

    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder();

        // Add Minio using the default endpoint
        builder.Services.AddMinio(accessKey, secretKey);

        // Add Minio using the custom endpoint and configure additional settings for default MinioClient initialization
        builder.Services.AddMinio(configureClient => configureClient
            .WithEndpoint(endpoint)
            .WithCredentials(accessKey, secretKey)
            .Build());

        // NOTE: SSL and Build are called by the build-in services already.

        var app = builder.Build();
        app.Run();
    }
}

[ApiController]
public class ExampleController : ControllerBase
{
    private readonly IMinioClient minioClient;

    public ExampleController(IMinioClient minioClient)
    {
        this.minioClient = minioClient;
    }

    [HttpGet]
    [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetUrl(string bucketID)
    {
        return Ok(await minioClient.PresignedGetObjectAsync(new PresignedGetObjectArgs()
                .WithBucket(bucketID))
            .ConfigureAwait(false));
    }
}

[ApiController]
public class ExampleFactoryController : ControllerBase
{
    private readonly IMinioClientFactory minioClientFactory;

    public ExampleFactoryController(IMinioClientFactory minioClientFactory)
    {
        this.minioClientFactory = minioClientFactory;
    }

    [HttpGet]
    [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetUrl(string bucketID)
    {
        var minioClient = minioClientFactory.CreateClient(); //Has optional argument to configure specifics

        return Ok(await minioClient.PresignedGetObjectAsync(new PresignedGetObjectArgs()
                .WithBucket(bucketID))
            .ConfigureAwait(false));
    }
}

MinIO 客户端示例

要连接到与 Amazon S3 兼容的云存储服务,您需要以下信息

变量名

描述

endpoint

<域名> 或 <ip:port>您的对象存储

accessKey

唯一标识您的帐户的用户 ID

secretKey

您帐户的密码

secure

启用/禁用 HTTPS 支持的布尔值(默认=true)

以下示例使用免费托管的公共 MinIO 服务“play.min.io”用于开发目的。

using Minio;

var endpoint = "play.min.io";
var accessKey = "minioadmin";
var secretKey = "minioadmin";
var secure = true;
// Initialize the client with access credentials.
private static IMinioClient minio = new MinioClient()
                                    .WithEndpoint(endpoint)
                                    .WithCredentials(accessKey, secretKey)
                                    .WithSSL(secure)
                                    .Build();

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

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

完整的文件上传器示例

此示例程序连接到对象存储服务器,创建一个存储桶并将文件上传到该存储桶。要运行以下示例,请单击 [链接] 并启动项目

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

namespace FileUploader
{
    class FileUpload
    {
        static void Main(string[] args)
        {
            var endpoint  = "play.min.io";
            var accessKey = "minioadmin";
            var secretKey = "minioadmin";
            try
            {
                var minio = new MinioClient()
                                    .WithEndpoint(endpoint)
                                    .WithCredentials(accessKey, secretKey)
                                    .WithSSL()
                                    .Build();
                FileUpload.Run(minio).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        // File uploader task.
        private async static Task Run(IMinioClient 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.
                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);
            }
        }
    }
}

运行 MinIO 客户端示例

在 Windows 上

  • 克隆此存储库并在 Visual Studio 2017 中打开 Minio.Sln。

  • 在 Minio.Examples/Program.cs 中输入您的凭据、存储桶名称、对象名称等。

  • 取消注释 Program.cs 中的以下示例测试用例,以运行示例。

  //Cases.MakeBucket.Run(minioClient, bucketName).Wait();
  • 从 Visual Studio 运行 Minio.Client.Examples 项目

在 Linux 上

在 Linux 上设置 .NET SDK(Ubuntu 22.04)
注意:minio-dotnet 需要 .NET 6.x SDK 才能在 Linux 上构建。
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0
运行 Minio.Examples
  • 克隆此项目。

$ git clone https://github.com/minio/minio-dotnet && cd minio-dotnet
  • 在 Minio.Examples/Program.cs 中输入您的凭据、存储桶名称、对象名称等。取消注释 Program.cs 中的以下示例测试用例,以运行示例。

  //Cases.MakeBucket.Run(minioClient, bucketName).Wait();
dotnet build --configuration Release --no-restore
dotnet pack ./Minio/Minio.csproj --no-build --configuration Release --output ./artifacts
dotnet test ./Minio.Tests/Minio.Tests.csproj
存储桶操作
存储桶策略操作
存储桶通知操作
文件对象操作
对象操作
预签名操作
客户端自定义设置

进一步探索