Quick Solutions to Fix Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials

Python is an indispensable programming language that has been the life and soul of programmers as they couldn’t make the web applications the way they are designing, and we would be able to see the web pages the way we are looking now. When working with Python, you may get ‘Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials’ error warning.

You must get worried when you encounter a such long error after compiling the code. Although the error seems long, the solutions we are going to highlight in this article will help you get rid of the error warning. Take a look at how you land up in the error warning

How the error shows up

You get the error as soon as you run the following code

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)

You must have saved your credentials in ~aws\credentials where the BOTO can read it.

As a result, you get this output in return

boto3.set_stream_logger('botocore', level='DEBUG')

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

This is how you get the error warning shown.

Solutions to fix ‘Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials’

There are a few amazing sure short solutions that can make the error warning go away. Check out the solutions

Avoid specify ACCESS_KEY and ACCESS_ID Directly

You need to avoid including ACCESS_KEY and ACCESS_ID directly in your code regarding security. You can specify the keys manually. It is recommended to use environment configurations, and then insert those into the code.

s3 = boto3.resource('s3',
         aws_access_key_id=ACCESS_ID,
         aws_secret_access_key= ACCESS_KEY)

Set profile name as ‘Default’

The error can also be caused when the format of ~/.aws/credentials is wrong. Set the profile name as ‘Default’ instead of ‘credentials’. With this, you can solve the error. Have a look at the code

[default]
aws_access_key_id=XXXXXXXXXXXXXX
aws_secret_access_key=YYYYYYYYYYYYYYYYYYYYYYYYYYY

Setting up credentials

You need to make sure ~/.aws/credentials in Unix can be like this

[MyProfile1]
aws_access_key_id = yourAccessId
aws_secret_access_key = yourSecretKey

[MyProfile2]
aws_access_key_id = yourAccessId
aws_secret_access_key = yourSecretKey

 with this, you have your Python code looks like this

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'])

Create an S3 Client Object

Another way to fix the error, you need to make an S3 client object with the credentials. It is always beneficial when you get the credentials from the OS environment. Take a look at the below code

AWS_S3_CREDS = {
    "aws_access_key_id":"your access key", # os.getenv("AWS_ACCESS_KEY")
    "aws_secret_access_key":"your aws secret key" # os.getenv("AWS_SECRET_KEY")
}
s3_client = boto3.client('s3',**AWS_S3_CREDS)

You can set the environment for the different operating systems. Check out the command

For Windows

c:System\> set AWS_ACCESS_KEY="aws_access_key"
c:System\> set AWS_SECRET_KEY="aws_secret_key"

For Mac or Linux

$ export AWS_ACCESS_KEY="aws_access_key"
$ export AWS_SECRET_KEY="aws_secret_key"

Conclusion

Here, we discussed the solutions to fix ‘Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials’. Different ways are described to help you remove the error warning. With that, I wish you all the best and happy coding!

Reference Source: https://jtuto.com/python-boto3-getting-error-botocore-exceptions-nocredentialserror-unable-to-locate-credentials/

https://www.anycodings.com/1questions/1663057/botocoreexceptionsnocredentialserror-unable-to-locate-credentials-github-actions

Leave a Reply

Your email address will not be published. Required fields are marked *