braaannigan
braaannigan

Reputation: 874

S3 Access Denied with boto for private bucket as root user

I am trying to access a private S3 bucket that I've created in the console with boto3. However, when I try any action e.g. to list the bucket contents, I get

boto3.setup_default_session()
s3Client = boto3.client('s3')
blist = s3Client.list_objects(Bucket=f'{bucketName}')['Contents']

ClientError: An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied

I am using my default profile (no need for IAM roles). The Access Control List on the browser states that the bucket owner has list/read/write permissions. The canonical id listed as the bucket owner is the same as the canonical id I get when I go to 'Your Security Credentials'.

In short, it feels like the account permissions are ok, but boto is not logging in with the right profile. In addition, running similar commands from the command line e.g.

aws s3api list-buckets

also gives Access Denied. I have no problem running these commands at work, where I have a work log-in and IAM roles. It's just running them on my personal 'default' profile.

Any suggestions?

Upvotes: 1

Views: 588

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269101

It appears that your credentials have not been stored in a configuration file.

You can run this AWS CLI command:

aws configure

It will then prompt you for Access Key and Secret Key, then will store them in the ~.aws/credentials file. That file is automatically used by the AWS CLI and boto3.

It is a good idea to confirm that it works via the AWS CLI first, then you will know that it should work for boto3 also.

I would highly recommend that you create IAM credentials and use them instead of root credentials. It is quite dangerous if the root credentials are compromised. A good practice is to create an IAM User for specific applications, then limit the permissions granted to that application. This avoids situations where a programming error (or a security compromise) could lead to unwanted behaviour (eg resources being used or data being deleted).

Upvotes: 1

Related Questions