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?
"Resource": [ "arn:aws:s3:::{bucket-name}", "arn:aws:s3:::{bucket-name}/*" ]
in your policy. The second form refers to objects, the first one to the bucket itself. – Michael - sqlbot