Movatterモバイル変換


[0]ホーム

URL:


Close Sidebar

.NET SDK

MinIO Client SDK provides higher level APIs for MinIO and Amazon S3 compatible cloud storage services.For a complete list of APIs and examples, please take a look at theDotnet Client API Reference.This document assumes that you have a working VisualStudio development environment.

Install from NuGet

To installMinIO .NET package, run the following command in Nuget Package Manager Console.

PM>Install-PackageMinio

MinIO client example for ASP.NET

When usingAddMinio to add Minio to your ServiceCollection, Minio will also use any custom Logging providers you’ve added, like Serilog to output traces when enabled.

usingMinio;usingMinio.DataModel.Args;publicstaticclassProgram{varendpoint="play.min.io";varaccessKey="minioadmin";varsecretKey="minioadmin";publicstaticvoidMain(string[]args){varbuilder=WebApplication.CreateBuilder();// Add Minio using the default endpointbuilder.Services.AddMinio(accessKey,secretKey);// Add Minio using the custom endpoint and configure additional settings for default MinioClient initializationbuilder.Services.AddMinio(configureClient=>configureClient.WithEndpoint(endpoint).WithCredentials(accessKey,secretKey).Build());// NOTE: SSL and Build are called by the build-in services already.varapp=builder.Build();app.Run();}}[ApiController]publicclassExampleController:ControllerBase{privatereadonlyIMinioClientminioClient;publicExampleController(IMinioClientminioClient){this.minioClient=minioClient;}    [HttpGet]    [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]publicasyncTask<IActionResult>GetUrl(stringbucketID){returnOk(awaitminioClient.PresignedGetObjectAsync(newPresignedGetObjectArgs().WithBucket(bucketID)).ConfigureAwait(false));}}[ApiController]publicclassExampleFactoryController:ControllerBase{privatereadonlyIMinioClientFactoryminioClientFactory;publicExampleFactoryController(IMinioClientFactoryminioClientFactory){this.minioClientFactory=minioClientFactory;}    [HttpGet]    [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]publicasyncTask<IActionResult>GetUrl(stringbucketID){varminioClient=minioClientFactory.CreateClient();//Has optional argument to configure specificsreturnOk(awaitminioClient.PresignedGetObjectAsync(newPresignedGetObjectArgs().WithBucket(bucketID)).ConfigureAwait(false));}}

MinIO client example

To connect to an Amazon S3 compatible cloud storage service, you need the following information

Variable nameDescription
endpoint<Domain-name> or <ip:port> of your object storage
accessKeyUser ID that uniquely identifies your account
secretKeyPassword to your account
secureboolean value to enable/disable HTTPS support (default=true)

The following examples uses a freely hosted public MinIO service “play.min.io” for development purposes.

usingMinio;varendpoint="play.min.io";varaccessKey="minioadmin";varsecretKey="minioadmin";varsecure=true;// Initialize the client with access credentials.privatestaticIMinioClientminio=newMinioClient().WithEndpoint(endpoint).WithCredentials(accessKey,secretKey).WithSSL(secure).Build();// Create an async task for listing buckets.vargetListBucketsTask=awaitminio.ListBucketsAsync().ConfigureAwait(false);// Iterate over the list of buckets.foreach(varbucketingetListBucketsTask.Result.Buckets){Console.WriteLine(bucket.Name+" "+bucket.CreationDateDateTime);}

Complete file uploader example

This example program connects to an object storage server, creates a bucket and uploads a file to the bucket.To run the following example, click on [Link] and start the project

usingSystem;usingMinio;usingMinio.Exceptions;usingMinio.DataModel;usingMinio.Credentials;usingMinio.DataModel.Args;usingSystem.Threading.Tasks;namespaceFileUploader{classFileUpload{staticvoidMain(string[]args){varendpoint="play.min.io";varaccessKey="minioadmin";varsecretKey="minioadmin";try{varminio=newMinioClient().WithEndpoint(endpoint).WithCredentials(accessKey,secretKey).WithSSL().Build();FileUpload.Run(minio).Wait();}catch(Exceptionex){Console.WriteLine(ex.Message);}Console.ReadLine();}// File uploader task.privateasyncstaticTaskRun(IMinioClientminio){varbucketName="mymusic";varlocation="us-east-1";varobjectName="golden-oldies.zip";varfilePath="C:\\Users\\username\\Downloads\\golden_oldies.mp3";varcontentType="application/zip";try{// Make a bucket on the server, if not already present.varbeArgs=newBucketExistsArgs().WithBucket(bucketName);boolfound=awaitminio.BucketExistsAsync(beArgs).ConfigureAwait(false);if(!found){varmbArgs=newMakeBucketArgs().WithBucket(bucketName);awaitminio.MakeBucketAsync(mbArgs).ConfigureAwait(false);}// Upload a file to bucket.varputObjectArgs=newPutObjectArgs().WithBucket(bucketName).WithObject(objectName).WithFileName(filePath).WithContentType(contentType);awaitminio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);Console.WriteLine("Successfully uploaded "+objectName);}catch(MinioExceptione){Console.WriteLine("File Upload Error: {0}",e.Message);}}}}

Running MinIO client examples

On Windows

  • Clone this repository and open the Minio.Sln in Visual Studio 2017.

  • Enter your credentials and bucket name, object name etc. in Minio.Examples/Program.cs

  • Uncomment the example test cases such as below in Program.cs to run an example.

//Cases.MakeBucket.Run(minioClient, bucketName).Wait();
  • Run the Minio.Client.Examples project from Visual Studio

On Linux

Setting .NET SDK on Linux (Ubuntu 22.04)

NOTE: minio-dotnet requires .NET 6.x SDK to build on Linux.
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.debsudo dpkg -i packages-microsoft-prod.debrm 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

Running Minio.Examples

  • Clone this project.
$ git clone https://github.com/minio/minio-dotnet && cd minio-dotnet
  • Enter your credentials and bucket name, object name etc. in Minio.Examples/Program.csUncomment the example test cases such as below in Program.cs to run an example.
//Cases.MakeBucket.Run(minioClient, bucketName).Wait();
dotnet build --configuration Release --no-restoredotnet pack ./Minio/Minio.csproj --no-build --configuration Release --output ./artifactsdotnet test ./Minio.Tests/Minio.Tests.csproj

Bucket operations

Bucket policy operations

Bucket notification operations

File object operations

Object operations

Presigned operations

Client custom settings

Explore further

On this page

Page Feedback

Was this page helpful?

Thank you for your feedback!

How can we improve this page?

Optionally provide your email if you would like us to follow up on your feedbackPlease enter a valid email address

Sending feedback...

Failed to send feedback. Please try again.


[8]ページ先頭

©2009-2025 Movatter.jp