When I simply run the following code, I always gets this error.
s3 = boto3.resource('s3') bucket_name = "python-sdk-sample-%s" % uuid.uuid4() print("Creating new bucket with name:", bucket_name) s3.create_bucket(Bucket=bucket_name)
I have saved my credential file in
C:\Users\myname\.aws\credentials, from where Boto should read my credentials.
Is my setting wrong?
Here is the output from
2015-10-24 14:22:28,761 botocore.credentials [DEBUG] Skipping environment variable credential check because profile name was explicitly set. 2015-10-24 14:22:28,761 botocore.credentials [DEBUG] Looking for credentials via: env 2015-10-24 14:22:28,773 botocore.credentials [DEBUG] Looking for credentials via: shared-credentials-file 2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: config-file 2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: ec2-credentials-file 2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: boto-config 2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: iam-role
try specifying keys manually
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_ID, aws_secret_access_key= ACCESS_KEY)
Make sure you don't include your ACCESS_ID and ACCESS_KEY in the code directly for security concerns. Consider using environment configs and injecting them in the code as suggested by @Tiger_Mike.
For Prod environments consider using rotating access keys: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_RotateAccessKey
I had the same issue and found out that the format of my
~/.aws/credentials file was wrong.
It worked with a file containing:
[default] aws_access_key_id=XXXXXXXXXXXXXX aws_secret_access_key=YYYYYYYYYYYYYYYYYYYYYYYYYYY
Note that the profile name must be "
[default]". Some official documentation make reference to a profile named "
[credentials]", which did not work for me.
If you are looking for an alternative way, try adding your credentials using AmazonCLI
from the terminal type:-
then fill in your keys and region.
Make sure your ~/.aws/credentials file in Unix looks like this:
[MyProfile1] aws_access_key_id = yourAccessId aws_secret_access_key = yourSecretKey [MyProfile2] aws_access_key_id = yourAccessId aws_secret_access_key = yourSecretKey
Your Python script should look like this, and it'll work:
from __future__ import print_function import boto3 import os os.environ['AWS_PROFILE'] = "MyProfile1" os.environ['AWS_DEFAULT_REGION'] = "us-east-1" ec2 = boto3.client('ec2') # Retrieves all regions/endpoints that work with EC2 response = ec2.describe_regions() print('Regions:', response['Regions'])
These instructions are for windows machine with a single user profile for AWS. Make sure your
~/.aws/credentials file looks like this
[profile_name] aws_access_key_id = yourAccessId aws_secret_access_key = yourSecretKey
I had to set the
AWS_DEFAULT_PROFILEenvironment variable to
profile_name found in your credentials.
Then my python was able to connect. eg from here
import boto3 # Let's use Amazon S3 s3 = boto3.resource('s3') # Print out bucket names for bucket in s3.buckets.all(): print(bucket.name)
I work for a large corporation and encountered this same error, but needed a different work around. My issue was related to proxy settings. I had my proxy set up so I needed to set my no_proxy to whitelist AWS before I was able to get everything to work. You can set it in your bash script as well if you don't want to muddy up your Python code with os settings.
import os os.environ["NO_PROXY"] = "s3.amazonaws.com"
no_proxy = "s3.amazonaws.com"
The boto3 is looking for the credentials in the folder like
You should save two files in this folder
You may want to check out the general order in which boto3 searches for credentials in this link. Look under the Configuring Credentials sub heading.
If you're sure you configure your aws correctly, just make sure the user of the project can read from ./aws or just run your project as a root
If you have multiple aws profiles in
[Profile 1] aws_access_key_id = ******************* aws_secret_access_key = ****************************************** [Profile 2] aws_access_key_id = ******************* aws_secret_access_key = ******************************************
Follow two steps:
Make one you want to use as a default using
export AWS_DEFAULT_PROFILE=Profile 1command in terminal.
Make sure to run above command in the same terminal from where you use boto3 or you open an editor.[Understand the following scenario]
- If you have two terminal open called
- And you run the export command in
t1and you open JupyterLab or any other from
t2, you will get NoCredentialsError: Unable to locate credentials error.
- Run the export command in
t1and then open JupyterLab or any other from the same terminal