Home > Enterprise >  putting a file in S3 bucket in private subnet
putting a file in S3 bucket in private subnet

Time:10-29

I am struggling with this issue for past two days. I am trying to put a file in S3 bucket in the private subnet. Below is my code:

var s3Config = new AmazonS3Config() { ServiceURL = "https://myVPCnameamazonaws.com" };
            using (var cli = new AmazonS3Client(
               awsAccessKey,
               awsSecretKey,
               s3Config))

            {
                PutObjectRequest req = new PutObjectRequest()
                {
                    BucketName = "test",
                    ContentType = "image/jpg",
                    InputStream = st,
                    Key = fileName
                    
                };
                var response = cli.PutObject(req);

In the last line of my code, I get an error saying : HttpErrorResponseException: The remote server returned an error: (403) Forbidden. Inner Exception:

WebException: The remote server returned an error: (403) Forbidden.

enter image description here

I am not sure what code should I write to get rid of this error. I am using AWS tool Kit. Visual studio 2019..net framework 4.7.2

Any help with this will be greatly appreciated.

CodePudding user response:

The problem appears to be an AccessDenied response which suggests that your request reached S3 but your credentials do not have permission to upload an object to that S3 bucket.

Check the IAM policies associated with your AWS credentials. Do they have the necessary S3 permissions?

An appropriate policy would include something like this (assuming your bucket name is mybucket):

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "s3:PutObject",
            "s3:PutObjectAcl"
         ],
         "Resource":"arn:aws:s3:::mybucket/*"
      }
   ]
}
  • Related