0

I'm doing the IBM course Python for Data Science and on one of the modules, i keep receiving an error message. I am following the instructions to use s3 and putobject to save a file to a bucket on IBM cloud object storage. It gives me a NoSuchKey error but the key is defined higher up

bucket_name = 'm4week5' 
html_path = '/home/dsxuser/work/index.html'
f = open(html_path,'r')
# Fill up the parameters in the following function:
# resource.Bucket(name=).put_object(Key=, Body=)
resource.Bucket(name = bucket_name).put_object(Key = html_path, Body = f.read())

Error message:


NoSuchKey                                 Traceback (most recent call last)
<ipython-input-22-a728805063e5> in <module>()
      1 # Fill up the parameters in the following function:
      2 # resource.Bucket(name=).put_object(Key=, Body=)
----> 3 resource.Bucket(name = bucket_name).put_object(Key = html_path, Body = f.read())

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/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'):

/opt/conda/envs/DSX-Python35/lib/python3.5/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)(**params)
     84 
     85         logger.debug('Response: %r', response)

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    310                     "%s() only accepts keyword arguments." % py_operation_name)
    311             # The "self" in this scope is referring to the BaseClient.
--> 312             return self._make_api_call(operation_name, kwargs)
    313 
    314         _api_call.__name__ = str(py_operation_name)

/opt/conda/envs/DSX-Python35/lib/python3.5/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    599             error_code = parsed_response.get("Error", {}).get("Code")
    600             error_class = self.exceptions.from_code(error_code)
--> 601             raise error_class(parsed_response, operation_name)
    602         else:
    603             return parsed_response

NoSuchKey: An error occurred (NoSuchKey) when calling the PutObject operation: The specified key does not exist.
AdamHutch
  • 5
  • 4

2 Answers2

5

You are getting this error because you have different endpoint of cloud storage

Goto buckets in IBM Watson studio select bucket endpoints option copy a public endpoint URL and replace it in your notebook at endpoint variable make sure https:// is there.

change the Key parameter as well file_name = 'index.html' Key = file_name

0

You can easily see the error thrown by the Python interpreter :

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

You are getting this error because the putObject is not able to find the key specified i.e html_path. The parameter 'key' should be the name of the HTML file which is 'index.html'. So your call should be something like :

resource.Bucket(name=bucket_name).put_object(Key='index.html', Body=f.read())

Ensure the following points as well :

  • Endpoint name should start with https:// and should be a string. You can select from any of the available public endpoints on Buckets Configuration page in IBM Watson studio.
  • If you are getting Access Denied error. Ensure you have created an access policy for the bucket.