DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Alex
Alex

Posted on

MinIO SDK Using REST API .NET For Amazon S3 Storage and MinIO

MinIO SDK Using REST API .NET For Amazon S3 Storage and MinIO
Object storage defined
Object storage is a data storage architecture for large stores of unstructured data. It designates each piece of data as an object, keeps it in a separate storehouse, and bundles it with metadata and a unique identifier for easy access and retrieval.
MinIO
MinIO offers high-performance, S3 compatible object storage.
Native to Kubernetes, MinIO is the only object storage suite available on
every public cloud, every Kubernetes distribution, the private cloud and the
edge. MinIO is software-defined and is 100% open source under GNU AGPL v3.
We will get started by create new .NET 5 project and Create Api ,then we will get to MinIO SDK, after which we will Create Service Minio and run Get and put command object with it.
Github Project : https://github.com/AlexGreatDev/AmazonS3
Create new .NET 5 project
Following commands create a new Web API with .NET 5 CLI:
#Create the API
dotnet new webapi -o AmazonS3
cd AmazonS3

Create a new file ObjectController.cs in the Controllers folder . This will add 2 new requests:
POST request at object/
GET request at object?objectname=@objectid&bucket=@bucketid
Install MinIO
To install MinIO .NET package, run the following command in Nuget Package Manager Console.
PM> Install-Package Minio
Create Service Minio
Create class MinioObject.cs in AmazonS3.Services.Minio Folder.
constructor:
private MinioClient _minio;
public MinioObject()
{
_minio = new MinioClient()
.WithEndpoint("Address")
.WithCredentials("YOUR-ACCESSKEYID",
"YOUR-SECRETACCESSKEY")
.WithSSL()//if Domain is SSL
.Build();
}

Put object :
public async Task<string> PutObj(PutObjectRequest request)
{
var bucketName = request.bucket;
// Check Exists bucket
bool found = await _minio.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName));
if (!found)
{
// if bucket not Exists,make bucket
await _minio.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName));
}
System.IO.MemoryStream filestream = new System.IO.MemoryStream(request.data);
var filename = Guid.NewGuid();
// upload object
await _minio.PutObjectAsync(new PutObjectArgs()
.WithBucket(bucketName).WithFileName(filename.ToString())
.WithStreamData(filestream).WithObjectSize(filestream.Length)
);
return await Task.FromResult<string>(filename.ToString());
}
Get Object
public async Task<GetObjectReply> GetObject(string bucket, string objectname)
{
MemoryStream destination = new MemoryStream();
// Check Exists object
var objstatreply= await _minio.StatObjectAsync(new StatObjectArgs()
.WithBucket(bucket)
.WithObject(objectname)
);
if (objstatreply == null || objstatreply.DeleteMarker)
throw new Exception(β€œobject not found or Deleted”);
// Get object
await _minio.GetObjectAsync(new GetObjectArgs()
.WithBucket(bucket)
.WithObject(objectname)
.WithCallbackStream((stream) =>
{
stream.CopyTo(destination);
}
)
);
return await Task.FromResult<GetObjectReply>(new GetObjectReply()
{
data = destination.ToArray(),
objectstat = objstatreply
});
}

Models :
using Minio.DataModel;
namespace AmazonS3.Services.Minio.Model
{
public class GetObjectReply
{
public ObjectStat objectstat { get; set; }
public byte[] data { get; set; }
}
}

And
namespace AmazonS3.Services.Minio.Model
{
public class PutObjectRequest
{
public string bucket { get; set; }
public byte[] data { get; set; }
}
}

Then add Call Minio Service in ObjectController.cs
private readonly ILogger<ObjectController> _logger;
private readonly MinioObject _minio;
public ObjectController(ILogger<ObjectController> logger, MinioObject minio)
{
_logger = logger;
_minio = minio;
}
[HttpGet]
public async Task<ActionResult> Get(string objectname, UploadTypeList bucket)
{
var result = await _minio.GetObject(bucket.ToString(), objectname);
return File(result.data, result.objectstat.ContentType);
}
[HttpPost]
public async Task<ActionResult> Post(UploadRequest request)
{
var result = await _minio.PutObj(new Services.Minio.Model.PutObjectRequest()
{
bucket = request.type.ToString(),
data = request.data
});
return Ok(new { filename = result });
}
and Add Minio service in IOC for DI in Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<MinioObject>();
}

Summary
Keep in mind that the Service is only available for .NET 5 or Higher. If you would like to use it in .NET Core, you have to use AmazonS3_NetCore Solution.
Here is the repo from the project, I hope you liked it. Happy coding!

Top comments (0)

πŸ€” Did you know?

Β 
πŸ“š You can adjust your experience level in Settings to see more relevant content.