Q:

Python boto connecting to S3 throwing error

Hello I am trying to connect Python to S3 in Frankfurt region using boto 2.43 where I want to print contents of bucket name... 
Following is my code :
from boto.s3.connection import S3Connection
hostname='s3.eu-central-1.amazonaws.com'
conn = S3Connection(aws_access_key_id,aws_secret_access_key, host=hostname)
bucket_name = conn.get_bucket('jd-eu01-isg-analytics-data-from-us01', validate=False)
for key in bucket_name.list(prefix='EU_Scripts_For_Frankfurt/'):
    print key
    continue

When I am executing it,it throws following error :
    File "/usr/lib/python2.7/site-packages/boto/s3/bucketlistresultset.py", line 34, in bucket_lister
    encoding_type=encoding_type)
  File "/usr/lib/python2.7/site-packages/boto/s3/bucket.py", line 473, in get_all_keys
    '', headers, **params)
  File "/usr/lib/python2.7/site-packages/boto/s3/bucket.py", line 399, in _get_all
    query_args=query_args)
  File "/usr/lib/python2.7/site-packages/boto/s3/connection.py", line 668, in make_request
    retry_handler=retry_handler
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1071, in make_request
    retry_handler=retry_handler)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 927, in _mexe
    request.authorize(connection=self)
  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 377, in authorize
    connection._auth_handler.add_auth(self, **kwargs)
  File "/usr/lib/python2.7/site-packages/boto/auth.py", line 727, in add_auth
    **kwargs)
  File "/usr/lib/python2.7/site-packages/boto/auth.py", line 546, in add_auth
    string_to_sign = self.string_to_sign(req, canonical_request)
  File "/usr/lib/python2.7/site-packages/boto/auth.py", line 486, in string_to_sign
    sts.append(self.credential_scope(http_request))
  File "/usr/lib/python2.7/site-packages/boto/auth.py", line 468, in credential_scope
    region_name = self.determine_region_name(http_request.host)
  File "/usr/lib/python2.7/site-packages/boto/auth.py", line 662, in determine_region_name
    return region_name
UnboundLocalError: local variable 'region_name' referenced before assignment

How to resolve this issue ?? is this because of boto version ?? Any solutions please

A:

Use boto3, it is easier. boto2 is not supported by AWS and boto already deprecated and there is no intention for features 
The error pop up because it can't find the region. Boto/boto3 
API  will check the region name inside the boto/boto3 service initialization stage. If you didn't specify it, it will look for default region name define inside credential file or environment variable (e.g. ~/.aws/config). 
This is true even you explicitly specify S3 endpoint URL. If you don't want to hard code the credential, region name, then you must setup AWS credential as specified here: credential configuration. 
Making boto/boto3 using credential file/environment variable will make your code cleaner and more flexible, e.g. you can even using STS without changing the code. e.g.: 
import boto3
# You can choose between service resource or service client.
s3 = boto3.client("s3")

response = s3.list_objects_v2(
  Bucket="jd-eu01-isg-analytics-data-from-us01", 
  Prefix="EU_Scripts_For_Frankfurt"
)

for content in response["Contents"]:
    print content["Key"]

Nevertheless, you can still hardcode access key id, secre_key, region name, etc by passing the parameter when you initialize boto/boto3 resources. (boto API is similar) 
import boto3
s3 = boto3.client(
  "s3",
  region_name = "eu-central-1",
  aws_access_key_id = 'xxxxxxxx",
  aws_secret_access_key= = "yyyyyyy")

