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

Eva*_*mir 5 python amazon-s3 boto docker boto3

当我在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                          
Run Code Online (Sandbox Code Playgroud)

Zei*_*ssS 1

我猜你没有设置正确的环境变量。用于env检查主机上的设置并在容器内设置类似的变量或使用-eto传递它们docker run

编辑:由于您在注释中指定您正在使用凭据文件,因此请将其传递到容器中-v ~/.aws/credentials:/root/.aws/credentialsHOME这假设,设置了正确的并且您正在使用该root用户。有些图像没有这样做,您可能需要将其放入根文件夹中/.aws/credentials。如果您有特定用户,则路径需要位于他的主文件夹中。