Sujeet Raman

Sujeet Raman

  • 857
  • 927
  • 353.8k

Issue with creating a folder in amazon s3 bucket using .net coe 6

Mar 6 2024 6:29 AM

Hi,

I want to create a folder in s3 bucket using an api like /create_folder

i am using Minio client for this

i have a repository class s3 repository and i am getting some issues related to minio client and couldnt resolve.can any one help?

using Minio;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace FileServiceAPI1._0.Repository
{
    public class S3Repository : IS3Repository
    {
        private const string S3ConfigSectionKey = "S3";
        private const string S3EndpointKey = "Endpoint";
        private const string S3BucketNameKey = "BucketName";
        private const string S3AccessKeyKey = "AccessKey";
        private const string S3SecretKeyKey = "SecretKey";

        private readonly string _bucketName;
        private readonly MinioClient _s3Client;
        private readonly ILogger _log;

        public S3Repository(ILogger<S3Repository> logger, IConfiguration configuration)
        {
            _log = logger;

            var s3Config = configuration.GetSection(S3ConfigSectionKey);
            var endpoint = s3Config.GetValue<string>(S3EndpointKey);
            var accessKey = s3Config.GetValue<string>(S3AccessKeyKey);
            var secretKey = s3Config.GetValue<string>(S3SecretKeyKey);
            _bucketName = s3Config.GetValue<string>(S3BucketNameKey);

            _s3Client = new MinioClient(endpoint, accessKey, secretKey);
        }

        public async Task<string> CreateFolderAsync(string folderName)
        {
            try
            {
                bool folderExists = await _s3Client.BucketExistsAsync(_bucketName);
                if (!folderExists)
                {
                    await _s3Client.MakeBucketAsync(_bucketName);
                }

                // Creating folder by adding a dummy file with the folder name as the prefix
                string objectName = folderName.TrimEnd('/') + "/";
                await _s3Client.PutObjectAsync(_bucketName, objectName, new MemoryStream(Encoding.UTF8.GetBytes("")));

                return $"Folder '{folderName}' created successfully.";
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Error creating folder");
                throw;
            }
        }
    }
}





using System.Threading.Tasks;

namespace FileServiceAPI1._0.Repository
{
    public interface IS3Repository
    {
        Task<string> CreateFolderAsync(string folderName);
    }
}






      

Errors I am getting in above code is 

  1. Minio client does not contain a constructor that takes 3 arguments
  2. cannot convert string to minio data model
  3. no overload method for await _s3Client.PutObjectAsync(_bucketName, objectName, new MemoryStream(Encoding.UTF8.GetBytes("")));

how can i fix?


Answers (2)