首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >no NoSuchKey:调用GetObject操作时出错(NoSuchKey):指定的键不存在

no NoSuchKey:调用GetObject操作时出错(NoSuchKey):指定的键不存在
EN

Stack Overflow用户
提问于 2021-02-08 21:06:06
回答 1查看 1.2K关注 0票数 1

我试图从s3访问一个文件,但一直收到以下错误:

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

然后,我尝试获取该对象

代码语言:javascript
复制
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)

我做错了什么?完整回溯:

代码语言:javascript
复制
---------------------------------------------------------------------------
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-08 22:50:25

问题出在:

代码语言:javascript
复制
folder_for_resources = "s3://test_bucket/development/resources/"

这应该设置为:

代码语言:javascript
复制
folder_for_resources = "development/resources/"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66102242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档