如何使用Boto3创建s3存储桶? [英] How to create a s3 bucket using Boto3?

查看:296
本文介绍了如何使用Boto3创建s3存储桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的帐户启用cloudtrail日志,因此需要创建一个s3存储桶.我想使用Boto3自动化此任务.当前我正在使用以下脚本

I want to enable cloudtrail logs for my account and so need to create an s3 bucket.I wanted to automate this task using Boto3.Currently I am using the following script

sess = Session(aws_access_key_id=tmp_access_key,
                   aws_secret_access_key=tmp_secret_key, aws_session_token=security_token)  
s3_conn_boto3 = sess.client(service_name='s3', region_name=region)  

bucket = s3_conn_boto3.create_bucket(Bucket=access_log_bucket_name,
                                                     CreateBucketConfiguration={'LocationConstraint':'us-east-1'},
                                                     ACL='authenticated-read',..).

我是Boto3的新手,所以我对使用其他参数(例如 GrantWrite GrantWriteACP 等)不了解.

I am new to Boto3 so I don't have much knowledge regarding usage of other parameters like GrantWrite,GrantWriteACP etc..

请帮助我提供一些有关创建s3存储桶并在其中启用cloudtrail日志的代码段.

Please help me provide some code snippet regarding create s3 bucket and enabled cloudtrail logs in it.

谢谢

推荐答案

阅读以下文档

http://boto3.readthedocs.io/en/latest/guide/migrations3.html

创建连接

Boto 3同时具有低级客户端和高级资源.对于Amazon S3,更高级别的资源与Boto 2.x的s3模块最相似:

Boto 3 has both low-level clients and higher-level resources. For Amazon S3, the higher-level resources are the most similar to Boto 2.x's s3 module:

Boto 2.x 导入boto

s3_connection = boto.connect_s3()

Boto 3

import boto3

s3 = boto3.resource('s3')

创建存储桶

Creating a Bucket

在Boto 2和Boto 3中创建存储区非常相似,除了在Boto 3中必须通过关键字参数传递所有操作参数,并且必须手动指定存储区配置:

Creating a bucket in Boto 2 and Boto 3 is very similar, except that in Boto 3 all action parameters must be passed via keyword arguments and a bucket configuration must be specified manually:

Boto 2.x

s3_connection.create_bucket('mybucket')

s3_connection.create_bucket('mybucket', location=Location.USWest)

Boto 3

s3.create_bucket(Bucket='mybucket')

s3.create_bucket(Bucket='mybucket', CreateBucketConfiguration={
    'LocationConstraint': 'us-west-1'})

存储数据

Storing Data

从文件,流或字符串中存储数据很容易:

Storing data from a file, stream, or string is easy:

Boto 2.x

from boto.s3.key import Key

key = Key('hello.txt')

key.set_contents_from_file('/tmp/hello.txt')

Boto 3

s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))

这篇关于如何使用Boto3创建s3存储桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆