Minio .NET Client SDK提供了简单的API来访问Minio以及任何与Amazon S3兼容的对象存储服务。有关API和示例的完整列表,请查看Dotnet Client API Reference文档。本文假设你已经有VisualStudio开发环境。
- .NET 4.5.2,.NetStandard1.6或更高版本
- Visual Studio 2017 RC
为了安装.NET Framework的Minio .NET包,你可以在Nuget Package Manager控制台运行下面的命令。
PM> Install-Package Minio
为了安装.NetCore的Minio .NET包,你可以在Nuget Package Manager控制台运行下面的命令。
PM> Install-Package Minio.NetCore
Minio client需要以下4个参数来连接与Amazon S3兼容的对象存储服务。
参数 | 描述 |
---|---|
endpoint | 对象存储服务的URL |
accessKey | Access key是唯一标识你的账户的用户ID。 |
secretKey | Secret key是你账户的密码。 |
secure | true代表使用HTTPS。 |
下面示例中使用运行在 https://play.minio.io:9000 上的Minio服务,你可以用这个服务来开发和测试。示例中的访问凭据是公开的。
using Minio;
// Initialize the client with access credentials.
private static MinioClient minio = new MinioClient("play.minio.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.CreationDate.DateTime);
}
本示例程序连接到一个对象存储服务,创建一个存储桶,并且上传一个文件到该存储桶中。为了运行下面的示例,请点击[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.minio.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);
}
}
}
}
- clone这个项目,并在Visual Studio 2017中打开Minio.Sln。
$ git clone https://github.com/minio/minio-dotnet && cd minio-dotnet
- 在Minio.Examples/Program.cs中输入你的认证信息、存储桶名称、对象名称等。 在Program.cs中取消注释以下类似的测试用例来运行示例。
//Cases.MakeBucket.Run(minioClient, bucketName).Wait();
- 从Visual Studio运行Minio.Client.Examples.NET452或Minio.Client.Examples.NetCore项目
注意:minio-dotnet需要mono 5.0.1稳定版本和.NET Core 1.0 SDK。
- 为你的发行版发装.NETCore和Mono 。请参阅示例脚本Ubuntu Xenial mono_install.sh安装.NETCore和Mono。
$ ./mono_install.sh
- 运行.NET4.5.2示例
$ mono nuget.exe restore
$ msbuild /p:Configuration=Release.Net452 /t:Clean
$ msbuild /p:Configuration=Release.Net452
$ ./Minio.Examples/Minio.Client.Examples.Net452/bin/Release.Net452/Minio.Client.Examples.Net452.exe
- 运行.NetCore示例
$ dotnet msbuild /p:Configuration=Release.Core
$ cd Minio.Examples/Minio.Client.Examples.Core
$ dotnet restore
$ dotnet run
- MakeBucket.cs
- ListBuckets.cs
- BucketExists.cs
- RemoveBucket.cs
- ListObjects.cs
- ListIncompleteUploads.cs