Trying to get location of bucket

165 Views Asked by At

I am new with this stuff, I want to get the location of s3 bucket but I still have this error "NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials" even I am using access key and secret key ? code is like this..

 from boto3.session import Session
   import boto
         
   ACCESS_KEY_ID = '*****'
   SECRET_KEY = '*****'
    
    session = Session(aws_access_key_id = ACCESS_KEY_ID, aws_secret_access_key = SECRET_KEY)
 
    s3 = session.resource('s3')
    bucket = 's3://****'
    my_bucket = s3.Bucket(bucket)
    
    **conn = boto.connect_s3()
    bucket = conn.get_bucket('*****')
    bucket_location = my_bucket.get_location()
    if bucket_location:
        conn = boto.s3.connect_to_region(bucket_location)
        bucket = conn.get_bucket('*****')**
1

There are 1 best solutions below

2
On

You can just use get_bucket_location:

import boto3

session = boto3.session.Session(aws_access_key_id = ACCESS_KEY_ID, aws_secret_access_key = SECRET_KEY)

s3 = session.client('s3')

response = s3.get_bucket_location(
    Bucket='bucket-name' 
)

print(response['LocationConstraint'])