22

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

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 的新手,所以我对使用其他参数(如GrantWriteGrantWriteACP等)知之甚少。

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

谢谢

4

4 回答 4

19

浏览以下文档

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

创建连接

Boto 3 既有低级客户端,也有高级资源。对于 Amazon S3,更高级的资源最类似于 Boto 2.x 的 s3 模块:

Boto 2.x 导入 boto

s3_connection = boto.connect_s3()

博托 3

import boto3

s3 = boto3.resource('s3')

创建存储桶

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

博托 2.x

s3_connection.create_bucket('mybucket')

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

博托 3

s3.create_bucket(Bucket='mybucket')

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

存储数据

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

博托 2.x

from boto.s3.key import Key

key = Key('hello.txt')

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

博托 3

s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
于 2016-08-30T09:08:37.547 回答
9

首先,在 boto3 中,如果您使用“aws configure”设置安全性,则无需声明“sess”部分(http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-入门.html )

# if you already done aws configure
import boto3
s3 = boto3.client("s3")
s3.create_bucket(Bucket="mybucket", ....) 

其次,糟糕的 boto3 文档无法链接正确的信息。这可以在 boto3 pdf 第 2181 页 ( https://media.readthedocs.org/pdf/boto3/latest/boto3.pdf )下找到

Email:Grantee 对象中的值是 AWS 账户的注册电子邮件地址。

Grantee:您希望有权访问转码文件和播放列表的 AWS 用户或组。要识别用户或组,您可以指定 AWS 账户的规范用户 ID、CloudFront 分配的源访问身份、AWS 账户的注册电子邮件地址或预定义的 Amazon S3 组

更简单的解决方案是使用策略设置 ( http://support.cloudcheckr.com/getting-started-with-cloudcheckr/preparing-your-aws-account/aggregate-cloudtrail/ )。您可以使用 put_bucket_policy() 转换整个内容,跳过可怕的 GrantWrite,GrantWriteACP

于 2016-03-16T19:41:09.700 回答
3

要在 AWS 上使用 Python 创建 S3 存储桶,您需要具有“aws_access_key_id_value”和“aws_secret_access_key_value”。您可以将此类变量存储在 config.properties 中,并将代码写入 create-s3-blucket.py 文件

创建一个 config.properties 并将以下代码保存在其中。

aws_access_key_id_value='YOUR-ACCESS-KEY-OF-THE-AWS-ACCOUNT'
aws_secret_access_key_value='TOUR-SECRETE-KEY-OF-THE-AWS-ACCOUNT'
Bucket_value='S3-BUCKET-NAME'
LocationConstraint_value='REGION-FOR-S3-BUCKET'

创建 create-s3-blucket.py 并在其中保存以下代码。

import boto3

def getVarFromFile(filename):
    import imp
    f = open(filename)
    global data
    data = imp.load_source('data', '', f)
    f.close()

getVarFromFile('config.properties')

client = boto3.client(
    's3',
    aws_access_key_id=data.aws_access_key_id_value,
    aws_secret_access_key=data.aws_secret_access_key_value
)
client.create_bucket(Bucket=data.Bucket_value, CreateBucketConfiguration={'LocationConstraint': data.LocationConstraint_value})

使用以下命令执行 python 代码。

python create-s3-blucket.py

同理,您可以添加不同的参数并自定义此代码。更多了解请参考AWS 的官方文档

于 2020-04-14T10:35:32.673 回答
-5
import boto3

client = boto3.client('s3')

response = client.create_bucket(
    ACL='private'|'public-read'|'public-read-write'|'authenticated-read',
    Bucket='string',
    CreateBucketConfiguration={
       'LocationConstraint': 'EU'|'eu-west-1'|'us-west-1'|'us-west-2'|'ap-south-1'|'ap-southeast-1'|'ap-southeast-2'|'ap-northeast-1'|'sa-east-1'|'cn-north-1'|'eu-central-1'
},
GrantFullControl='string',
GrantRead='string',
GrantReadACP='string',
GrantWrite='string',
GrantWriteACP='string')
于 2016-12-25T05:48:04.573 回答