skip to Main Content

Context: I have configured right aws-access-key and aws-secret-key
I can see buckets contents on aws-console but on aws-cli

Here’s my boto3 code

import boto3

# Enter the name of your S3 bucket here
bucket_name = 'xxxx'

# Enter the name of the region where your S3 bucket is located
region_name = 'ap-southeast-1'

# Create an S3 client
s3 = boto3.client('s3', region_name=region_name)

# List all the objects in the bucket
objects = s3.list_objects(Bucket=bucket_name)

# Print the names of all the objects in the bucket
for object in objects['Contents']:
    print(object['Key'])

I have "s3:List*" under my AWS-policy. What am I missing?

I am trying list all buckets using aws-cli it works using aws-console but not cli. I have rechecked my aws-secret/acess key, everything is right.

EDIT: aws-cli throws error

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

2

Answers


  1. Usually this kind of error happens if you have multiple AWS profiles in your PC and you are using the wrong profile to make a call a call to AWS
    Make sure the default profile is the profile that has access to the AWS account.

    You can also use aws s3 ls --PROFILE_NAME to get the list of buckets if you have multiple profiles

    Run aws sts get-caller identity to get the current caller identity

    Login or Signup to reply.
  2. From ListBuckets – Amazon Simple Storage Service:

    Returns a list of all buckets owned by the authenticated sender of the request. To use this operation, you must have the s3:ListAllMyBuckets permission.

    The wording is a bit confusing, but:

    • ListBuckets returns a list of the names of S3 buckets in your AWS Account
    • ListObjects returns a list of objects in a particular S3 bucket

    Your Python code is calling list_objects().

    The AWS CLI error is saying ListBuckets operation: Access Denied, which suggests that you are trying to obtain a list of buckets, rather than a list of objects.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search