.NET 快速入門指南
適用於 .NET 的 MinIO 用戶端 SDK
MinIO 用戶端 SDK 為 MinIO 和 Amazon S3 相容的雲端儲存服務提供更高等級的 API。如需完整的 API 和範例清單,請查看Dotnet 用戶端 API 參考。本文件假設您擁有可用的 VisualStudio 開發環境。
從 NuGet 安裝
若要安裝MinIO .NET 套件,請在 Nuget 套件管理員主控台中執行以下命令。
PM> Install-Package Minio
適用於 ASP.NET 的 MinIO 用戶端範例
當使用 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 相容的雲端儲存服務,您需要以下資訊
變數名稱 |
描述 |
---|---|
端點 |
您的物件儲存的 <網域名稱> 或 <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 (Ubuntu 22.04) 上設定 .NET SDK
注意:minio-dotnet 需要 .NET 6.x SDK 才能在 Linux 上建置。
安裝.Net SDK
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