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

Viewed 4870

I am trying to access a file from s3 but i keep getting the error:

NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.
import boto3
s3 = boto3.resource('s3')
bucket_name = "test_bucket"
folder_for_resources = "s3://test_bucket/development/resources/"
bucket = s3.Bucket(bucket_name)

I then try to get the object

obj=s3.Object(bucket_name,f"{folder_for_resources}test.json")
file_content = obj.get()['Body'].read().decode('utf-8')
a2c_old = json.load(file_content)

What am I doing wrong? full traceback:

---------------------------------------------------------------------------
NoSuchKey                                 Traceback (most recent call last)
<ipython-input-11-1a964eacf78b> in <module>
      1 print(f"{folder_for_resources}test.json")
      2 obj=s3.Object(bucket_name,f"{folder_for_resources}test.json")
----> 3 file_content = obj.get()['Body'].read().decode('utf-8')
      4 a2c_old = json.load(file_content)

~/.pyenv/versions/3.6.10/lib/python3.6/site-packagtestes/boto3/resources/factory.py in do_action(self, *args, **kwargs)
    518             # instance via ``self``.
    519             def do_action(self, *args, **kwargs):
--> 520                 response = action(self, *args, **kwargs)
    521 
    522                 if hasattr(self, 'load'):

~/.pyenv/versions/3.6.10/lib/python3.6/site-packages/boto3/resources/action.py in __call__(self, parent, *args, **kwargs)
     81                     operation_name, params)
     82 
---> 83         response = getattr(parent.meta.client, operation_name)(*args, **params)
     84 
     85         logger.debug('Response: %r', response)

~/.pyenv/versions/3.6.10/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    355                     "%s() only accepts keyword arguments." % py_operation_name)
    356             # The "self" in this scope is referring to the BaseClient.
--> 357             return self._make_api_call(operation_name, kwargs)
    358 
    359         _api_call.__name__ = str(py_operation_name)

~/.pyenv/versions/3.6.10/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    674             error_code = parsed_response.get("Error", {}).get("Code")
    675             error_class = self.exceptions.from_code(error_code)
--> 676             raise error_class(parsed_response, operation_name)
    677         else:
    678             return parsed_response

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

The issue was with:

folder_for_resources = "s3://test_bucket/development/resources/"

This should have been set as:

folder_for_resources = "development/resources/"
Related