2017-09-04 207 views
0

我想从我的亚马逊s3存储桶之一下载文件到Ubuntu EC2 instance.I在两个不同区域创建了两个存储桶,我可以从一个区域的存储桶访问和下载文件,但是当我尝试访问其他人面临的问题。aws s3下载文件

Here is the error, 
Traceback (most recent call last): 
    File "listbucket.py", line 7, in <module> 
    bucket = conn.get_bucket('train-faces') 
    File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 509, in get_bucket 
    return self.head_bucket(bucket_name, headers=headers) 
    File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 556, in head_bucket 
    response.status, response.reason, body) 
boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request 

但我可以从另一个区域的桶下载文件。

+0

当你设置你的AWS凭证你应该设置默认区域。这可能是为什么它为一个而不是另一个工作。你在命令中指定区域吗?例如。 --region us-east-1 – slipperyseal

+0

没有iam没有在命令中指定任何区域,但我已配置区域。 – Raj

+0

但如果我改变任何其他地区的配置其不工作 – Raj

回答

1

您可以在与Amazon S3连接时指定区域。

博托

import boto 
conn = boto.s3.connect_to_region('ap-southeast-2') 
conn.get_bucket('train-faces') 

boto3

这些天来,我们建议您使用boto3

import boto3 
s3 = boto3.resource('s3', region_name = 'ap-southeast-2') 
bucket = s3.Bucket('train-faces') 

import boto3 
client = boto3.client('s3', region_name = 'ap-southeast-2') 
+0

非常感谢!我得到了它的工作 – Raj

0

您可以使用下面的命令来下载

import boto3 
s3 = boto3.client('s3','region_name') 
s3.download_file("bucket-name", "key-name", "tmp.txt") 

请参阅http://boto3.readthedocs.io/en/latest/guide/s3.html

解释 - 我们首先做一个连接到特定区域的S3。您从一个Bucket获取文件的原因可能是您在配置boto时设置的值。在建立连接时提供region_name是个好习惯。 另一个原因可能是文件无法访问或权限被拒绝。如果以后是这个问题,那么你应该是同样的错误说权限被拒绝