简体   繁体   English

从EC2实例访问Amazon S3 Bucket

[英]Access to Amazon S3 Bucket from EC2 instance

I have an EC2 instance and an S3 bucket in different region. 我在不同的区域有一个EC2实例和一个S3存储桶。 The bucket contains some files that are used regularly by my EC2 instance. 存储桶包含我的EC2实例定期使用的一些文件。 I want to programatically download the files on my EC2 instance (using python) 我想以编程方式下载我的EC2实例上的文件(使用python)

Is there a way to do that? 有没有办法做到这一点?

Lots of ways to do this from within python 在python中有很多方法可以做到这一点

Boto has S3 modules which will do this. Boto有S3模块可以做到这一点。 http://boto.readthedocs.org/en/latest/ref/s3.html http://boto.readthedocs.org/en/latest/ref/s3.html

You could also just use the python requests library to download over http 您也可以使用python请求库通过http下载

AWS Cli also give you an option to download from the shell: AWS Cli还为您提供从shell下载的选项:

aws s3 cp s3://bucket/folder/file.name file.name

Adding to what @joeButler has said above... 添加@joeButler上面所说的...

Your instances need permission to access S3 using APIs. 您的实例需要使用API​​访问S3的权限。 So, you need to create IAM role and instance profile. 因此,您需要创建IAM角色和实例配置文件。 Your instance needs to have instance profile assigned when it is being created. 您的实例需要在创建实例时分配实例配置文件。 See page 183 (as indicated on bottom of page. The topic name is "Using an IAM Role to Grant Permissions to Applications Running on Amazon EC2 Instances") of this guide: AWS IAM User Guide to understand the steps and procedure. 请参阅本指南的第183页(如页面底部所示。主题名称为“使用IAM角色为在Amazon EC2实例上运行的应用程序授予权限”): AWS IAM用户指南,以了解步骤和过程。

I work for Minio, its open source, S3 Compatible object storage written in golang. 我为Minio工作,它的开源,用Golang编写的S3 Compatible对象存储。

You can use minio-py client library, its open source & compatible with AWS S3. 您可以使用minio-py客户端库,其开源并与AWS S3兼容。 Below is a simple example of get_object.py 下面是get_object.py的一个简单示例

from minio import Minio
from minio.error import ResponseError

client = Minio('s3.amazonaws.com',
               access_key='YOUR-ACCESSKEYID',
               secret_key='YOUR-SECRETACCESSKEY')

# Get a full object
try:
    data = client.get_object('my-bucketname', 'my-objectname')
    with open('my-testfile', 'wb') as file_data:
        for d in data:
            file_data.write(d)
except ResponseError as err:
    print(err)

You can also use minio client aka mc it come mc mirror command to perform the same. 你也可以使用minio client aka mc it come mc mirror命令来执行相同的操作。 You can add it to cron. 您可以将其添加到cron。

$ mc mirror s3/mybucket localfolder

Note: 注意:

  • s3 is an alias s3是别名
  • mybucket is your AWS S3 bucket mybucket是您的AWS S3存储桶
  • localfolder is EC2 machine file for backup. localfolder是用于备份的EC2机器文件。

Installing Minio Client: 安装Minio客户端:

GNU/Linux GNU / Linux的

Download mc for: 下载mc:

$ ./mc config host add mys3 https://s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12

Note: Replace access & secret key with yours. 注意:用您的访问权限和密钥替换。

As mentioned above, you can do this with Boto. 如上所述,您可以使用Boto执行此操作。 To make it more secure and not worry about the user credentials, you could use IAM to grant the EC2 machine access to the specific bucket only. 为了使其更安全而不用担心用户凭据,您可以使用IAM仅授予EC2计算机对特定存储桶的访问权限。 Hope that helps. 希望有所帮助。

If you want to use python, you may want to use the newer boto3 API. 如果你想使用python,你可能想要使用更新的boto3 API。 I personally like it more than to original boto package. 我个人比原来的boto包更喜欢它。 It works with both python2 and python3 and the differences are minimal. 它适用于python2和python3, 差异很小。

You can specify region when you create a new bucket (see boto3.Client documentation), but bucket names are unique, so you shouldn't need one to connect to it. 您可以在创建新存储桶时指定区域(请参阅boto3.Client文档),但存储桶名称是唯一的,因此您不需要连接它。 And you probably don't want to use bucket in different region than your instance because you will pay for data transfer between regions . 并且您可能不希望在不同的区域使用存储桶而不是实例,因为您将为区域之间的数据传输付费

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM