ShadyOverflow
ShadyOverflow

Reputation: 105

Upload file to Amazon S3 bucket

I know there are lot of threads like this, I tried like 100 examples, all of them throw the same exception:

The specified bucket is not valid.

I contacted the s3 admin, he said, everything is correct from his side, and he said, he set full permissions, no deny statements, so we can test, but my app still throws the same exception, here is the code..

using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;

namespace AmazonS3
{
    public class FileUploader
    {
        private const string S3_Bucket_ARN = "(hidden)";
        private const string IAM_User_ARN = "(hidden)";
        private const string Access_Key_ID = "(hidden)";
        private const string Secret_access_Key = "(hidden)";

        public void UploadFile(string Filepath)
        {
            Program.WriteLine(MessageStatus.Neutral, "[1/4] Connecting to Amazon S3 . . .");
            using (IAmazonS3 client = new AmazonS3Client(Access_Key_ID, Secret_access_Key, RegionEndpoint.USEast1))
            {
                Program.WriteLine(MessageStatus.Neutral, "[2/4] Preparing upload request . . .");
                var uploadRequest = new TransferUtilityUploadRequest
                {
                    FilePath = Filepath,
                    BucketName = S3_Bucket_ARN,
                    CannedACL = S3CannedACL.PublicRead
                };

                Program.WriteLine(MessageStatus.Neutral, $"[3/4] Uploading file: \"{Filepath}\" . . .");
                var fileTransferUtility = new TransferUtility(client);
                fileTransferUtility.Upload(uploadRequest);

                Program.WriteLine(MessageStatus.Neutral, $"[4/4] File: \"{Filepath}\" successfully uploaded.");
            }
        }
    }
}

the hidden strings are ofcourse replaced by my real credentials, I just edited them now. Anyway what seems incorrect here? Im stuck for days now, Thanks in advance.

Upvotes: 0

Views: 5844

Answers (2)

Michael - sqlbot
Michael - sqlbot

Reputation: 178956

The problem is here:

BucketName = S3_Bucket_ARN

ARNs are Amazon Resource Names, which are a namespace for uniquely identifying Amazon resources... but in this case, BucketName is expecting only the actual name of the bucket, e.g. my-example-bucket.

If you are using the bucket ARN, you are passing a value that looks like arn:aws:s3:::my-example-bucket, which is not only not the bucket name, but also contains characters (:) that aren't valid in bucket names.

The S3 API Reference indicates that there are two different errors possible when a bucket name isn't right:

InvalidBucketName | The specified bucket is not valid.
NoSuchBucket      | The specified bucket does not exist.

The first one means the bucket name contains invalid characters.

Upvotes: 1

aas
aas

Reputation: 197

I have used below method recently it may help

        private string UploadAWS(Stream stream, string contentType, string name,string orgfileName,string lable)
    {
        string accessKey = "xxxxx";
        string secretKey = "xxxxxxx";

        using (client = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast1))
        {
            return WritingAnObject(stream, contentType, name, orgfileName,lable);
        }
    }

    private string WritingAnObject(Stream stream, string contentType, string name,string orgfileName,string lable)
    {
        string rtn = string.Empty;

        try
        {
            string bucketName = "xxxxxxxx";

            PutObjectRequest putRequest2 = new PutObjectRequest
            {
                BucketName = bucketName,
                Key = name,
                ContentType = contentType,
                InputStream = stream
            };
            putRequest2.Metadata.Add("x-amz-meta-title", lable);
            putRequest2.Metadata.Add("x-amz-meta-original-file-name", orgfileName);

            PutObjectResponse response2 = client.PutObject(putRequest2);

        }
        catch (AmazonS3Exception amazonS3Exception)
        {
            if (amazonS3Exception.ErrorCode != null &&
                (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                ||
                amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
            {
                rtn="Check the provided AWS Credentials.";
            }
            else
            {
                rtn="Error occurred. Message:"+ amazonS3Exception.Message + " when writing an object";
            }
        }

        return rtn;

    }

Upvotes: 2

Related Questions