在 IBM Cloud Storage 上使用 S3 .putobject 时出现 NoSuchKey 错误

0 python ibm-watson

我正在学习 IBM 课程 Python for Data Science,在其中一个模块上,我不断收到一条错误消息。我按照说明使用 s3 和 putobject 将文件保存到 IBM 云对象存储上的存储桶。它给了我一个 NoSuchKey 错误,但密钥是在上面定义的

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())
Run Code Online (Sandbox Code Playgroud)

错误信息:


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.
Run Code Online (Sandbox Code Playgroud)

小智 5

您收到此错误是因为您有不同的云存储端点

转至 IBM Watson studio 中的存储桶,选择存储桶端点选项,复制公共端点 URL 并将其替换在笔记本中的端点变量处,确保 https:// 存在。

更改 Key 参数 file_name = 'index.html' Key = file_name