Unable to get object metadata from S3. Check object key, region and/or access permissions in aws Rekognition

Question:

import boto3

if __name__ == "__main__":

    bucket='MyBucketName'
sourceFile='pic1.jpg'
targetFile='pic2.jpg'

client=boto3.client('rekognition','us-east-1')

response=client.compare_faces(SimilarityThreshold=70,
                              SourceImage={'S3Object':{'Bucket':bucket,'Name':sourceFile}},
                              TargetImage={'S3Object':{'Bucket':bucket,'Name':targetFile}})

for faceMatch in response['FaceMatches']:
    position = faceMatch['Face']['BoundingBox']
    confidence = str(faceMatch['Face']['Confidence'])
    print('The face at ' +
           str(position['Left']) + ' ' +
           str(position['Top']) +
           ' matches with ' + confidence + '% confidence')

I am trying to compare two images present in my bucket but no matter which region i select i always get the following error:-

botocore.errorfactory.InvalidS3ObjectException: An error occurred (InvalidS3ObjectException) when calling the CompareFaces operation: Unable to get object metadata from S3. Check object key, region and/or access permissions.

My bucket’s region is us-east-1 and I have configured the same in my code.
what am I doing wrong?

Answers:

Please ensure the AWS environment variable configuration AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY in your script before compile

Answered By: Venkatesh Kuppusamy

It seems to me that you dont have enough permissions with that access_key and secret_key! If the credentials are of an IAM user, make sure the IAM user has permission to perform Rekognition compare_faces read operations and s3 read operations! Also check if your s3 source and target object key are correct.
And it is better to create roles with required permissions and assume that role to request temporary security credentials instead of using the permanent access keys.

Answered By: Manoj Acharya

I had the same problem. What I did to fix it was to rearrange my bucket and the folders. Make sure that your image is directly in your bucket and not in a folder in your bucket. Also double check that the name of the images are correct and that everything is on point.

Answered By: Sean Li

Also ran into this issue, noticed my IAM role had the bucketname as the resource, i had to add a slash and a wildcard to the end. changed it to “Resource”: “arn:aws:s3:::/*”

Answered By: Jeffrey DeMuth

Ensure bucket region is same as calling region. If you are using AWS CLI then make sure to include profile with appropriate region.

Answered By: Tejas Mahale

Check if the S3 and Image Rekognition is in the same region, I know, it’s not nice or documented (I guess), but this guys are talking about it here and here

Answered By: Italo José

It happened to me using the AWS rekognition sdk for android , the problem was that the region of the S3 bucket is not the same in my request , so I had to put the correct region in the request (same as S3 bucket ) :

 rekognitionClient.setRegion(Region.getRegion(Regions.US_WEST_1));//replace with your S3 region
Answered By: Mohammed Fathi

For me changing the file permissions in the S3 bucket worked.

Answered By: Gennaro

For me the problem was the name of the file in s3 Bucket containing Spaces. So you have to make sure the key doesn’t contain spaces while storing itself.

Answered By: Rajesh Kanna

I had the same error: I checked and found out that the image was present in some subfolder of the bucket. Make sure that the image is in the root bucket.

Answered By: yogender

In my case I had the path to my object prefixed with a slash (/). Removing it did the trick.

Answered By: spaceemotion

Although this is a very old question, but I also had the same issue. But In my case, I was using the Lambda and my Lambda role didn’t had the access to S3, so if you are doing it through Lambda, you need to provide the S3 access to it in addition to Rekognition.

Answered By: Shipra Sahu

Same error mesagge but using Textract functions, no problem with permissions, but my files in s3 containing special caracters, once I renamed the files there was no problem.

Answered By: Hermann

I faced a similar problem like

botocore.errorfactory.InvalidS3ObjectException: An error occurred
(InvalidS3ObjectException) when calling the CompareFaces operation: Unable to > get object metadata from S3. Check object key, region and/or access
permissions

It may be due to wrong AWS region, key or Permission was not given properly.

In my case the wrong region was set as an environment variable.

Answered By: Koushik Samanta

Ran into similar issue, and figured out it is due to having space in any of the folder name.

Answered By: SKannaian