在Docker中使用boto3从S3下载文件失败

当我在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 

看看错误: An error occurred (403) when calling the HeadObject operation: Forbidden

它find了凭据,但没有权限访问存储桶。 底线:更新您的IAM权限,以包含您的存储桶的s3:ListBucket权限: arn:aws:s3:::mybucket/*或仅将策略AmazonS3ReadOnlyAccess附加到您的IAM用户/angular色。

你可以尝试这个,看看它打印正确的凭据:

 >>> import botocore.session >>> session = botocore.session.get_session() >>> session.get_credentials().access_key 'AKIAABCDEF6RWSGI234Q' >>> session.get_credentials().secret_key 'abcdefghijkl+123456789+qbcd' 

我想你没有设置正确的环境variables。 使用env检查你的主机上设置了什么,并在容器中设置类似的variables,或通过-e传递给docker run

编辑:由于您在注释中指定了使用凭证文件,因此请使用-v ~/.aws/credentials:/root/.aws/credentials将其传递到容器中。 这假设,一个适当的HOME设置,你正在使用root用户。 有些图片还没有这样做,你可能需要把它放到/.aws/credentials的根目录下。 如果你有一个特定的用户,path需要在他的主文件夹。

什么解决了我的问题是在我的容器上安装awscli并更新boto3> 1.4

 pip install awscli pip install --upgrade boto3