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

NoSuchKey error when using S3 .putobject on IBM Cloud Storage

我正在学习有关数据科学的 IBM 课程 Python,并且在其中一个模块上,我不断收到一条错误消息。我按照说明使用 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())

错误信息:


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.

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

转到 IBM Watson 工作室中的存储桶 select 存储桶端点选项复制一个 public 端点 URL 并在您的笔记本中将其替换为端点变量,确保 https:// 存在。

同时更改 Key 参数 file_name = 'index.html' 键 = file_name

您可以很容易地看到 Python 解释器抛出的错误:

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

您收到此错误是因为 putObject 无法找到指定的密钥,即 html_path。参数 'key' 应该是 HTML 文件的名称,即 'index.html'。 所以你的电话应该是这样的:

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

还要确保以下几点:

  • 端点名称应以 https:// 开头且应为字符串。您可以从 IBM Watson studio Buckets Configuration 页面上的任何可用 public 端点 select。
  • 如果您收到 Access Denied 错误。确保您已为存储桶创建 access policy