How to push file in folder inside S3 bucket using local stack

I am using below code to push file in s3 bucket of AWS, It’s working fine

       `var folderPath = <Folder Name>;
        string fileName = <File name with path>; // test.json
        var bucketKey = folderPath + fileName;

        using var stream = domainItemErrorMessage.Message.ToStream();
        var putObjectRequest = new S3BucketItem
        {
            BucketName = <BucketName>,
            Key = bucketKey,
            FileContent = stream
        };`

But when I tries to use same code for local stack, it’s giving error as.

Amazon.S3.AmazonS3Exception: The specified bucket does not exist

But when I combine bucket name and folder name like and key separate its working fine on local stack

       `var folderPath = <Folder Name>;
        string fileName = <File name with path>; // test.json
        var bucketKey = fileName;

        using var stream = domainItemErrorMessage.Message.ToStream();
        var putObjectRequest = new S3BucketItem
        {
            BucketName = <BucketName>+"/"+folderPath,
            Key = bucketKey,
            FileContent = stream
        };`

but giving error on AWS s3 as

The request signature we calculated does not match the signature you provided. Check your key and signing method.

Hello @sachin,

Can you please confirm that you are following the configuration mentioned in .NET | Docs (localstack.cloud)?

Yes i added below code but still same issue

Code at config

 if (environment.Equals(ApplicationConstants.LocalEnv, StringComparison.CurrentCultureIgnoreCase))
        {
            var awsOptions = new AWSOptions();
            awsOptions.DefaultClientConfig.ServiceURL = ApplicationConstants.LocalStackURL;
            awsOptions.DefaultClientConfig.AuthenticationRegion = "ap-southeast-2";
            services.AddDefaultAWSOptions(awsOptions);
        }

at code
var folderPath = _s3BucketOptions.StorageFolder.EndsWith(“/”) ? _s3BucketOptions.StorageFolder : s3BucketOptions.StorageFolder + “/”;
string fileName = $"{domainItemErrorMessage.TopicName}
{dateHelper.UtcNow():yyyyMMddHHmmssfff}{domainItemErrorMessage.Key}.json";
var bucketKey = folderPath.Trim() + fileName.Trim();

        using var stream = domainItemErrorMessage.Message.ToStream();
        var putObjectRequest = new S3BucketItem
        {
            BucketName = _s3BucketOptions.BucketName.Trim(),
            Key = bucketKey,
            FileContent = stream
        };

        await _s3Client.StoreMessageAsync(putObjectRequest);

Hello @sachin,

Following the documentation linked above, it seems there’s still a part missing in your client initialization, namely the ForcePathStyle parameter which is why you’re getting an issue. Basically, by default, your client will prepend the bucket name to the host, and LocalStack won’t pick it up if you’re not setting ApplicationConstants.LocalStackURL to "http://s3.localhost.localstack.cloud:4566".

Anyway, could you give it a try with this configuration?

if (environment.Equals(ApplicationConstants.LocalEnv, StringComparison.CurrentCultureIgnoreCase))
        {
            var awsOptions = new AWSOptions();
            awsOptions.DefaultClientConfig.ServiceURL = ApplicationConstants.LocalStackURL;
            awsOptions.DefaultClientConfig.AuthenticationRegion = "ap-southeast-2";
            awsOptions.DefaultClientConfig.ForcePathStyle = true;
            services.AddDefaultAWSOptions(awsOptions);
        }

And see if that solves your issue?