Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 giving me NoSuchKey error even when the key exists

Tags:

This is my boto3 command for getting the object with a specific key from an S3 bucket:

resp = s3client.get_object(Bucket='<>-<>', Key='MzA1MjY1NzkzX2QudHh0') 

It gives the following error:

botocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist. 

I have checked in the bucket, and the key actually exists

enter image description here

Did I miss something or did I do something wrong here?

like image 382
Dawny33 Avatar asked Jun 27 '17 10:06

Dawny33


People also ask

How do I prevent accidental deletion of S3?

To prevent or mitigate future accidental deletions, consider the following features: Enable versioning to keep historical versions of an object. Enable Cross-Region Replication of objects. Enable MFA delete to require multi-factor authentication (MFA) when deleting an object version.

Does S3 Putobject overwrite?

If an object already exists in a bucket, the new object will overwrite it because Amazon S3 stores the last write request.

What is S3 prefix key?

A key prefix is a string of characters that can be the complete path in front of the object name (including the bucket name). For example, if an object (123. txt) is stored as BucketName/Project/WordFiles/123. txt, the prefix might be “BucketName/Project/WordFiles/123.


2 Answers

You have a %0A at the end of your URL; that's a line separator.

like image 164
Chris Pollard Avatar answered Oct 14 '22 05:10

Chris Pollard


Since you know the key that you have is definitely in the name of the file you are looking for, I recommend using a filter to get objects with names with your key as their prefix.

s3 = boto3.resource('s3') bucket = s3.Bucket('cypher-secondarybucket') for obj in bucket.objects.filter(Prefix='MzA1MjY1NzkzX2QudHh0'):     print obj.key 

When you run this code, you will get the key names of all the files that start with your key. This will help you find out what your file is exactly called on S3.

like image 26
cookiedough Avatar answered Oct 14 '22 07:10

cookiedough