'AWS S3 IllegalLocationConstraintException Error when using bucket.objects.all()

It is needed to read and download files from AWS S3 (not created by me, it is external S3 resource by others, so i cannot change any bucket policy) i am in Hong Kong (my physical location)

IllegalLocationConstraintException appears. Full Error is shown below (An error occurred (IllegalLocationConstraintException) when calling the ListObjects operation: The us-east-2 location constraint is incompatible for the region specific endpoint this request was sent to.

REGION_NAME = 'us-east-2'
client = boto3.client('s3',region_name=REGION_NAME) 
s3 = boto3.resource('s3',
                aws_access_key_id=ACCESS_KEY,
                aws_secret_access_key=SECRET_KEY
                ,region_name = REGION_NAME
)

bucket=s3.Bucket(BUCKET_NAME)

for obj in bucket.objects.all():
    print(obj)
    print(f'filename : {obj.key} ')
    s3.Bucket(BUCKET_NAME).download_file(obj.key, f'download_from_s3/{obj.key}')

I have tried to do the following but it doesn't help

  1. set region_name = 'us-east-2' and add this params
  2. Try call the following to look at the bucket info, still face same error IllegalLocationConstraintException

response = client.get_bucket_cors(Bucket=BUCKET_NAME)

result = client.get_bucket_policy(Bucket=BUCKET_NAME)

  1. Reach lots of online solution to set the LocationConstraint. But it seems for create bucket, i cannot change the bucket's policy (as created by others)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source