【问题标题】:How to create a s3 bucket using Boto3?如何使用 Boto3 创建 s3 存储桶?
【发布时间】:2015-09-14 12:31:33
【问题描述】:

我想为我的帐户启用 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 的新手,所以我对 GrantWrite、GrantWriteACP 等其他参数的使用了解不多。

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

谢谢

【问题讨论】:

  • 你的例子有什么问题?
  • 还尝试使用create_bucket 方法。只是我还是 Boto3 不好?什么样的文档是“字符串”??? "GrantFullControl='string'" 也许下一位会更清楚... ;) ... "GrantFullControl (string) -- 允许被授权者对存储桶进行读取、写入、读取 ACP 和写入 ACP 权限。"无论如何,对于咆哮感到抱歉...您是否找到了某人需要使用诸如 GrantFullControl 之类的参数的实际字符串? (请注意 GrantFull 中的完整“咆哮”;)

标签: python amazon-web-services amazon-s3 boto boto3


【解决方案1】:

首先,在 boto3 中,如果您使用“aws configure”设置安全性,则无需声明“sess”部分 (http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.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

【讨论】:

    【解决方案2】:

    阅读以下文档

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

    创建连接

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

    博托 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'))
    

    【讨论】:

      【解决方案3】:
      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')
      

      【讨论】:

      • 我不知道为什么这是最差的答案,它实际上是亚马逊的 boto3 文档的摘录。也很有帮助。
      • @neo7 没有任何用法解释的纯代码答案通常被否决。另外,如果是复制粘贴而不注明出处,那就不好了。
      【解决方案4】:

      要在 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's official documentation。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 2021-03-29
        • 2016-10-20
        • 1970-01-01
        • 2021-11-25
        • 2021-07-26
        • 1970-01-01
        相关资源
        最近更新 更多