1

I'm using

>>> s3 = session.client(service_name='s3',
... aws_access_key_id='access_key_id_goes_here',
... aws_secret_access_key='secret_key_goes_here',
... endpoint_url='endpoint_url_goes_here')
>>> s3.list_buckets() 

to list out my existing buckets, but got the error botocore.exceptions.ClientError: An error occurred () when calling the ListBuckets operation: Not sure how to proceed from that

Alexander
  • 59,041
  • 12
  • 98
  • 151
Jack Yu
  • 11
  • 2

1 Answers1

2

Are you using boto3?

Here is some sample code. There are two ways to use boto:

  • The 'client' method that maps to AWS API calls, or
  • The 'resource' method that is more Pythonic

boto3 will automatically retrieve your user credentials from a configuration file, so there is no need to put credentials in the code. You can create the configuration file with the AWS CLI aws configure command.

import boto3

# Using the 'client' method
s3_client = boto3.client('s3')

response = s3_client.list_buckets()

for bucket in response['Buckets']:
    print(bucket['Name'])

# Or, using the 'resource' method
s3_resource = boto3.resource('s3')

for bucket in s3_resource.buckets.all():
    print(bucket.name)

If you are using an S3-compatible service, you can add a endpoint_url parameter to the client() and resource() calls.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Yes I'm using boto3 and an S3-compatible service (OpenStack swift), however, when I followed your code I still got the same error. Maybe it's because of my swift configuration – Jack Yu Jul 05 '21 at 17:25