I've created a user with a policy having full access to S3:
When I set credentials (~/.aws/credentials) and try to push a file to my bucket using boto3, it returns An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
Any idea why is this happening?
I've even tried generating an access key id / secret access key for the root account and it returns the same error. I've run my code with another account's credentials and it works fine, so the issue is within the account itself.
EDIT:
I verified that the caller ID is the same as my account using boto3.client('sts').get_caller_identity().get('Account')
.
I don't have a policy set for my bucket, and these are its permissions
This is a snippet of my class
from boto3 import client
from botocore.exceptions import ClientError
from boto3.exceptions import S3UploadFailedError
class AmazonS3(object):
s3 = client('s3')
@classmethod
def upload_image(cls, bucket_name, object_name, file_content):
extra_args = {"ACL": "public-read",
"ContentType": "image/jpeg" if object_name.split('.')[-1] in ['jpg', 'jpeg'] else "image/png",
"ContentDisposition": "inline",
"ContentEncoding": "base64"}
try:
cls.s3.put_object(Body=file_content, Bucket=bucket_name, Key=object_name, **extra_args)
except (ClientError, S3UploadFailedError, Exception) as e:
raise Exception('There was an error when uploading the image')
aws sts get-caller-identity
and ensure the call is made by the right principal. Is there a bucket policy on the bucket? Please include any policies you set up in JSON form. – Maurice