user301340
user301340

Reputation: 60

C# with AWS S3 access denied with transfer utility

I've been trying for several hours to get this to work and not sure what I'm doing wrong. I have a bucket in US-West Oregon (us-west-2). I have an IAM user with full S3 access. I am attempting to store the contents of a string into a file on S3. Here is the code below:

using System.IO;
using Amazon;

namespace XXX.Util
{

    public static class S3
    {

        private static Amazon.S3.Transfer.TransferUtility transferUtility;

        public static void UploadFile(string bucket, string key, string contents)
        {    
            // user "publisher" credentials
            transferUtility = new Amazon.S3.Transfer.TransferUtility("{AccessID}", "{Secret}",RegionEndpoint.USWest2);

            using (Stream s = GenerateStreamFromString(contents))
            {
                using (transferUtility)
                {
                    transferUtility.Upload(s, bucket, key);
                }
            }


        }
        public static Stream GenerateStreamFromString(string s)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }
    }
}

I invoke the method with:

S3.UploadFile("s3-us-west-2.amazonaws.com/{bucket-name}", guid.ToString(),contents);

The error message I get is:

AmazonS3Exception: Access Denied Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException(IExecutionContext executionContext, HttpErrorResponseException exception) in HttpErrorResponseExceptionHandler.cs, line 60

My S3 policy:

{ "Version": "2012-10-17", "Id": "Policy1516569218147", "Statement": [ { "Sid": "Stmt1516569211561", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::{iamID}:user/publisher" }, "Action": "s3:*", "Resource": "arn:aws:s3:::{bucket-name}" } ] }

Any ideas?

Upvotes: 1

Views: 7277

Answers (1)

infojolt
infojolt

Reputation: 5418

According to the documentation:

The S3 location matches the format s3://bucket/path. Don't include the endpoint. For example, s3://us-east-1.amazonaws.com/bucket/path results in an "Access Denied" error.

Upvotes: 0

Related Questions