16
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?

amin saffar
  • 1,953
  • 3
  • 22
  • 34
  • 1
    Did you find a solution to this? I am getting same error with my bucket although same lambda work successfully with another bucket created by aws rekognition. – Noura Jul 02 '18 at 16:12
  • This code is working for me now, i did not change anything it just started working when i deployed it. – Harsh Vardhan Parashar Jul 05 '18 at 08:55
  • I figured the role was assigned to another bucket. It’s working now. – Noura Jul 10 '18 at 14:39
  • Could you give more details on how you solved it? This is kind of a general problem – John Nov 01 '18 at 17:57

15 Answers15

12

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.

Sean Li
  • 138
  • 2
  • 7
12

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

Italo José
  • 1,558
  • 1
  • 17
  • 50
4

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

mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26
  • yes i can second that. even though S3 is Global but it worked when i set the session to the same region as S3 – gurinder Nov 04 '21 at 16:25
3

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.

Koushik Samanta
  • 195
  • 1
  • 15
2

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
Mohammed Fathi
  • 1,237
  • 1
  • 14
  • 12
1

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.

Manoj Acharya
  • 1,331
  • 2
  • 15
  • 27
1

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.

Rajesh Kanna
  • 113
  • 6
1

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

spaceemotion
  • 1,404
  • 4
  • 24
  • 32
1

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.

Hermann
  • 11
  • 2
1

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

SKN
  • 11
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33554019) – sim Jan 05 '23 at 13:18
0

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

0

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:::/*"

Jeffrey DeMuth
  • 131
  • 1
  • 3
0

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

Gennaro
  • 138
  • 1
  • 2
  • 16
0

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.

yogender
  • 496
  • 4
  • 7
0

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.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 30 '22 at 14:48