Reputation: 5117
I am using AWS, boto3 and Pycharm to write a very simple program on python that compare one faces from one face image to another face from another face image. I written the following simple source code:
import boto3
s3 = boto3.resource('s3',
aws_access_key_id = "xxx",
aws_secret_access_key = "yyy")
BUCKET = "eyeglasses-images"
KEY_SOURCE = "Foucault.jpg"
KEY_TARGET = "Ricoeur.jpg"
def compare_faces(bucket, key, bucket_target, key_target, threshold=80, region="eu-west-1"):
rekognition = boto3.client("rekognition", region)
response = rekognition.compare_faces(
SourceImage={
"S3Object": {
"Bucket": bucket,
"Name": key,
}
},
TargetImage={
"S3Object": {
"Bucket": bucket_target,
"Name": key_target,
}
},
SimilarityThreshold=threshold,
)
return response['SourceImageFace'], response['FaceMatches']
source_face, matches = compare_faces(BUCKET, KEY_SOURCE, BUCKET, KEY_TARGET)
# the main source face
print
"Source Face ({Confidence}%)".format(**source_face)
# one match for each target face
for match in matches:
print
"Target Face ({Confidence}%)".format(**match['Face'])
print
" Similarity : {}%".format(match['Similarity'])
However I get the following error:
raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials
(Obviously in the place of xxx and yyy I am using the real keys)
What is the problem and how can I fix this?
Upvotes: 0
Views: 1364
Reputation: 7356
The issue is in the line: rekognition = boto3.client("rekognition", region)
Because you did not specify the keys here, boto3 is trying to find the credentials file in ~/.aws. Update this to use the session
object as mentioned by Aniket. Your code should change to:
session = boto3.session.Session(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY
)
rekognition = session.client("rekognition", region)
Upvotes: 0
Reputation: 68905
You can use session first. Eg.
session = boto3.Session(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
)
and then get your resource
s3 = session.resource('s3')
Note that aws_session_token is optional and will be used if you use STS to get temp sessions.
Just saying
s3 = boto3.client('s3')
s3 = boto3.resource('s3')
will use default creds in ~/.aws/credentials
file. So it cannot be used in your case.
It would be good if you can directly use -
s3 = boto3.client(
's3',
# Hard coded strings as credentials, not recommended.
aws_access_key_id='xx',
aws_secret_access_key='yy'
)
For more details refer - boto3 configuration documentation
Upvotes: 1