If you are using AWS S3 C# TransferUtilityUploadRequest and when you try to upload objects onto S3 you can potentially get Access Denied. The reason could your IAM Role is not defined to have access or your bucket name is incorrect.
What do you mean bucket name is incorrect? Basically it boils down to that buckets used with Amazon S3 Transfer Acceleration can’t have dots (.) in their names. So if you created a bucket with name e.g “my.fancy.bucket.” this will not work with TransferUtilityUploadRequest. You will need to change the name to “my-fancy-bucket” rather.
Example below I am using this code to upload some excel data.
using (var transferUtility = new TransferUtility(S3Client))
{
var request = new TransferUtilityUploadRequest
{
BucketName = "my.excel.bucket",
Key = "filename.xlsx",
InputStream = stream,
ContentType = "application/vnd.ms-excel",
AutoCloseStream = true
};
//the line below will throw an exception
transferUtility.Upload(request);
}
Now if you change the code to use dash for the bucket name it will succeed in uploading, make sure you do have a bucket with that name already created.
using (var transferUtility = new TransferUtility(S3Client))
{
var request = new TransferUtilityUploadRequest
{
BucketName = "my-excel-bucket",
Key = "filename.xlsx",
InputStream = stream,
ContentType = "application/vnd.ms-excel",
AutoCloseStream = true
};
//the line below will throw an exception
transferUtility.Upload(request);
}
Make sure to check the Role given to the lambda function also, use least privileges for s3 if possible to and if you are logging any information you need to give the Cloudwatch permission to your lambda also.
Hope this helps 🙂
View Comments (2)
Hello, How do we specify full path including the folder(s) name: is it like [bucket/folder-name] or [bucket/folder/]?
e.g. BucketName = "my-excel-bucket/folder-name"
I can access and upload files to the S3 bucket via AWS console but when I use the secret key and access key generated for the same user, in C# code, it returns "Access Denied".
Have you tried something like this.
// subdirectory and bucket name
request.BucketName = bucketName + @"/" + subDirectoryInBucket;