【问题标题】:HTTP Code 400 getting objectfrom AWS S3 bucket with Boto3 in pythonHTTP 代码 400 在 python 中使用 Boto3 从 AWS S3 存储桶获取对象
【发布时间】:2017-05-08 03:42:09
【问题描述】:

我正在编写一个脚本,该脚本应该从我的 s3 存储桶中下载文件,然后对其进行一些更改。 问题是我的脚本在以 root 身份执行时运行良好,但在使用专用用户执行时却不行

这是我们感兴趣的一段代码:

s3 = boto3.client('s3', aws_access_key_id='xxx', aws_secret_access_key='xxx')

s3.download_file('my-bucket', 'my_file.csv', '/my/path/to/file.csv')

当我以 root 身份运行脚本时,没问题,文件被下载并且脚本继续执行,但使用我的专用用户执行:

logstash:x:501:501::/home/logstash:/sbin/nologin,不行。

我尝试以 root 和 logstash 的身份运行 Python 解释器,并逐个命令执行脚本命令,它以 root 身份运行,但不能以 logstash 身份运行。这是错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/boto3/s3/inject.py", line 126, in download_file
extra_args=ExtraArgs, callback=Callback)
File "/usr/local/lib/python2.7/site-packages/boto3/s3/transfer.py", line 299, in download_file
future.result()
File "/usr/local/lib/python2.7/site-packages/s3transfer/futures.py", line 73, in result
return self._coordinator.result()
File "/usr/local/lib/python2.7/site-packages/s3transfer/futures.py", line 233, in result
raise self._exception
botocore.exceptions.ClientError: An error occurred (400) when calling the HeadObject operation: Bad Request

我在 google 上搜索了很多,但没有关于这个特殊的“用户切换”案例的主题。

【问题讨论】:

  • 你关注了the steps以logstash登录了吗?
  • 不,因为我不以 root 身份使用它们(我也不想这样做)。我使用硬编码凭据来实现可移植性(我知道这是不好的做法)...凭据在 boto3.client 参数中
  • 很奇怪,它对我有用。
  • 也许该异常具有误导性,并且您无权写入从其中启动 python 脚本的目录...
  • 我太愚蠢了...chown -R logstash: . 解决了我的问题。我一定忘记了之前chown 上的-R 选项。无论如何,谢谢!

标签: python amazon-s3 boto aws-cli boto3


【解决方案1】:

我是第一个:)

用户logstash 的权限是否为您要下载到的目录正确设置?

你有没有错过chown -R logstash中的-R

【讨论】:

  • 遗憾的是,这不是解决方案。因为当我完成chown -R 后,我的下载目录中已经有了file.csv(之前以root 身份下载)。所以我也更改了它的权限,但是作为logstash执行的脚本无法自行下载文件并使用以前的文件(作为root和更改的所有者)继续该过程......
  • 您是否尝试过chown -R logstash:logstash 用于用户和组?
  • 是的,这就是我在您之前的回答chown -R logstash: 的评论中所写的内容,我认为也是如此。
  • 不知道是不是一样。反正我会试试的:)
  • 对不起,我不知道,我认为原因可能是您用户的权限..
【解决方案2】:

以防其他人出于与我相同的原因遇到此错误: 对我来说,解决方法是在初始化 s3 客户端时提供一个 region,例如:

    s3 = boto3.client("s3", region_name="eu-central-1")

其中“eu-central-1”需要替换为存储桶所在的区域。 看起来是这个错误:https://github.com/boto/botocore/issues/1413,尽管听起来应该修复它。 仅供参考,由于未设置此区域而遇到的两个错误是

botocore.exceptions.ClientError: An error occurred (InvalidClientTokenId) when calling the GetCallerIdentity operation: The security token included in the request is invalid

问题中已经说明的那个:

botocore.exceptions.ClientError: An error occurred (400) when calling the HeadObject operation: Bad Request

有趣的是,没有那个,一些调用在“eu-central-1”中确实有效,而在中国,同样的请求在“cn-north-1”中失败了。

【讨论】:

    【解决方案3】:

    有同样的错误。问题出在文件路径不正确

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 1970-01-01
      • 2021-05-09
      • 2017-05-26
      • 1970-01-01
      • 2018-03-29
      • 2021-09-03
      • 1970-01-01
      • 2018-05-08
      相关资源
      最近更新 更多