当我在Docker容器中运行它时失败,但是当我在OS X中的virtualenv中运行它时工作正常。任何想法可能出错? Docker + boto有任何已知问题吗?
>>> import boto3
>>> s3 = boto3.client('s3')
>>> s3.download_file("mybucket", "myfile.txt", "myfile2.txt")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/conda/lib/python2.7/site-packages/boto3/s3/inject.py", line 104, in download_file
extra_args=ExtraArgs, callback=Callback)
File "/opt/conda/lib/python2.7/site-packages/boto3/s3/transfer.py", line 666, in download_file
object_size = self._object_size(bucket, key, extra_args)
File "/opt/conda/lib/python2.7/site-packages/boto3/s3/transfer.py", line 729, in _object_size
Bucket=bucket, Key=key, **extra_args)['ContentLength']
File "/opt/conda/lib/python2.7/site-packages/botocore/client.py", line 258, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/conda/lib/python2.7/site-packages/botocore/client.py", line 548, in _make_api_call
raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
答案 0 :(得分:2)
查看错误:An error occurred (403) when calling the HeadObject operation: Forbidden
它找到了凭据,但它没有访问存储桶的权限。结论:更新您的IAM权限以包含您的广告资源的s3:ListBucket
权限:arn:aws:s3:::mybucket/*
或者只需将政策AmazonS3ReadOnlyAccess
附加到您的IAM用户/角色。
您可以尝试这一点并看到它打印正确的凭据:
>>> import botocore.session
>>> session = botocore.session.get_session()
>>> session.get_credentials().access_key
'AKIAABCDEF6RWSGI234Q'
>>> session.get_credentials().secret_key
'abcdefghijkl+123456789+qbcd'
答案 1 :(得分:1)
我猜你没有设置正确的环境变量。使用MyLog("my message")
检查主机上设置的内容,并在容器中设置相似的变量,或者使用env
将其传递给-e
。
编辑:由于您在评论中指定了您使用的是凭据文件,因此请将其传递到包含docker run
的容器中。这假设设置了正确的-v ~/.aws/credentials:/root/.aws/credentials
并且您正在使用HOME
用户。有些图片没有这样做,您可能需要将它放在root
的根文件夹中。
如果您有特定用户,则路径必须位于其主文件夹中。
答案 2 :(得分:0)
为我解决的问题是在我的容器上安装awscli并更新boto3&gt; 1.4
pip install awscli
pip install --upgrade boto3
答案 3 :(得分:0)
我只是追查了一个类似的问题,这归结为一个事实,即在尝试与S3通信时返回403的机器上的系统时间是错误的。错误的系统时间意味着对请求的签名的计算不正确。将系统时间设置为正确的值可以解决问题-如果您确保docker容器设置了系统时间(例如,通过使用NTP),那么您的问题可能会像我的问题一样消失。
答案 4 :(得分:0)
我在Windows上运行的docker遇到了同样的问题。问题是,一旦您的PC进入待机模式,Hyper-V时间就会停止。这引起了问题,因为我的请求和AWS的存储桶时间戳都不同。
为我解决了问题的是仅重新启动docker /我的计算机...
还请记住,要发送您的AWS-配置文件/凭据作为环境变量。看来,这是来自docker的问题: