如何在 aws 中使用 boto3 创建不具有公共访问权限的存储桶和对象?

Mia*_*Mia 6 python amazon-s3 amazon-web-services boto3

我想创建一个存储桶和不具有公共访问权限的对象,即它应该使用 boto3 python 代码是私有的,我怎样才能实现它?

s3_client = boto3.client('s3', aws_access_key_id=accessKey,
                       aws_secret_access_key=secretKey,
                       region_name=region)
   

 s3_bucket= s3_client.create_bucket(Bucket=bucket_name)
    print('bucket created')
    print(s3_bucket)
    bucket_name = 'bucket123'
    response_public = s3_client.put_public_access_block(
        Bucket=bucket_name,
        PublicAccessBlockConfiguration={
            'BlockPublicAcls': True,
            'IgnorePublicAcls': True,
            'BlockPublicPolicy': True,
            'RestrictPublicBuckets': True
        },
    )
    print("pubic",response_public)
Run Code Online (Sandbox Code Playgroud)

存储桶已创建但正在获取

错误:在公共访问中,“S3”对象没有属性“put_public_access_block”

Chr*_*ams 6

编辑

由于最初认为 boto3 版本是旧版本 (1.9.42),因此您可以从本文档中看到,该功能在该版本中不可用。

原来的

我像这样运行它,一切都成功应用,我建议查看缩进并验证您正在运行的 Boto3 版本。

s3_client = boto3.client('s3', region_name='eu-west-1')
    bucket_name= 'ohfihfhfeuhehfuhfeih'
    
    s3_bucket= s3_client.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={'LocationConstraint': 'eu-west-1'})

    response_public = s3_client.put_public_access_block(
        Bucket=bucket_name,
        PublicAccessBlockConfiguration={
            'BlockPublicAcls': True,
            'IgnorePublicAcls': True,
            'BlockPublicPolicy': True,
            'RestrictPublicBuckets': True
        },
    )
Run Code Online (Sandbox Code Playgroud)

简而言之,您的代码很好(我添加了区域配置,因为某些区域需要这样做),还有另一个因素。修复缩进,检查您的 boto3 版本(如果是旧版本,请更新,当前版本是 1.14.7)。