2
Answers

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

Photo of Sujeet Raman

Sujeet Raman

1y
487
1

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);
    }
}
C#

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)

2
Photo of Naimish Makwana
133 13.8k 201.5k 1y

It seems like you’re having trouble with the Minio client in your .NET Core 6 application. Let’s address each of your issues:

  1. Minio client does not contain a constructor that takes 3 arguments: The MinioClient constructor requires a MinioConfiguration object as an argument. You can create this configuration object and pass it to the MinioClient constructor like this:
    var minioConfig = new MinioConfiguration
    {
        Endpoint = endpoint,
        AccessKey = accessKey,
        SecretKey = secretKey
    };
    _s3Client = new MinioClient(minioConfig);
    
  2. Cannot convert string to minio data model: This error usually occurs when you’re trying to pass a string where a Minio data model is expected. Without more specific information about where this error is occurring, it’s hard to provide a precise solution. Please check the Minio documentation or the method signature to ensure you’re passing the correct types of arguments.

  3. No overload method for await _s3Client.PutObjectAsync(_bucketName, objectName, new MemoryStream(Encoding.UTF8.GetBytes("")));: The PutObjectAsync method expects a PutObjectArgs object. You can create this object and pass it to the method like this:

    var putObjectArgs = new PutObjectArgs(_bucketName, objectName, new MemoryStream(Encoding.UTF8.GetBytes("")))
    {
        ContentType = "application/octet-stream"
    };
    await _s3Client.PutObjectAsync(putObjectArgs);
    

Thanks

0
Photo of Sujeet Raman
823 927 376.3k 1y

i got almost same answer from chat GPT but still it is not working thanks