AWS BOTO3 S3 python - 调用 HeadObject 操作时发生错误 (404):未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46635895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:44:31  来源:igfitidea点击:

AWS BOTO3 S3 python - An error occurred (404) when calling the HeadObject operation: Not Found

pythonamazon-web-servicesamazon-s3error-handlingboto3

提问by Hyman

I am trying download a directory inside s3 bucket. I am trying to use transfer to download a directory from S3 bucket but I am getting an error as "An error occurred (404) when calling the HeadObject operation: Not Found". Please help.

我正在尝试在 s3 存储桶中下载一个目录。我正在尝试使用 transfer 从 S3 存储桶下载目录,但出现错误“调用 HeadObject 操作时发生错误 (404):未找到”。请帮忙。

S3 structure:
  **Bucket
     Folder1
        File1**

Note: Trying to download Folder1

注意:尝试下载 Folder1

transfer.download_file(self.bucket_name, self.dir_name, self.file_dir + self.dir_name)

回答by Marcus Vinicius Melo

I had the same issue recently. You are probably misspelling the path and folder name. In my case, for example, I was messing up with the '/'.

我最近遇到了同样的问题。您可能拼错了路径和文件夹名称。例如,在我的情况下,我弄乱了“/”。

To fix it, make sure the variables you are using as arguments for the function contains the correct names of the directories, folders and files as it is in S3. Also, make sure you put the '/' in the correct places in the correct variables. For instance, in my case I found that:

要修复它,请确保用作函数参数的变量包含正确的目录、文件夹和文件名称,就像在 S3 中一样。另外,请确保将“/”放在正确变量中的正确位置。例如,就我而言,我发现:

  • bucket name: bucket_name(with no '/' in the end, and no 's3://')
  • directory name: folder1/folder2/file_name(with no '/' in the beginning)
  • 存储桶名称:bucket_name(末尾没有“/”,也没有“s3://”)
  • 目录名:folder1/folder2/file_name(开头没有'/')

I hope it helps you and others to get around this error easily.

我希望它可以帮助您和其他人轻松解决此错误。

回答by Avidan Efody

Another possible cause that I've run into is that the file you're trying to download contains 0 bytes. This is pretty confusing as AWS cli will download it without any objections

我遇到的另一个可能原因是您尝试下载的文件包含 0 个字节。这非常令人困惑,因为 AWS cli 会毫无异议地下载它

回答by Shreyas

Yet another possibly is that you entered an incorrect endpoint_urlparameter when creating your S3 resource.

另一种可能是您endpoint_url在创建 S3 资源时输入了错误的参数。

For future users, create your resource like this:

对于未来的用户,请像这样创建您的资源:

s3 = boto3.resource(
  's3',
  region_name=[your region, e.g. eu-central-1],
  aws_access_key_id=[your access key],
  aws_secret_access_key=[your secret key]
)

In the above, it is possible to pass an endpoint_url, as I erroneously did (I later found out that I had accidentally passed the endpoint URL to a different AWS service).

在上面,有可能传递一个endpoint_url,因为我错误地做了(我后来发现我不小心将端点 URL 传递给了不同的 AWS 服务)。

If you are using AWS CLI in order to authenticate, you can omit the region_name, aws_access_key, and aws_secret_access_keyparameters, like so:

如果您正在使用AWS CLI,以便进行身份验证,则可以省略region_nameaws_access_key以及aws_secret_access_key参数,如下所示:

s3 = boto3.resource('s3')