我想创建一个没有公共访问权限的存储桶和对象,即应该使用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)
创建桶但获得
错误:在公共访问中作为“ S3”对象没有属性“ put_public_access_block”
原因可能是由于您正在创建名为的存储桶
bucket_name= 's3buck123'
但是您使用其他名称来设置put_public_access_block
bucket_name = 'bucket123'
缩进也是错误的,但是我认为这是由于复制粘贴到SO。
修改
最初认为boto3版本是旧版(1.9.42),因此从this documentation中可以看到,该功能在该版本中不可用。
原始
我以这种方式运行它,并且一切都成功应用,建议您查看缩进并验证您正在运行的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
},
)
简而言之,您的代码很好(我添加了区域配置,因为某些区域需要这样做),还有另一个因素。修复缩进,检查您的boto3版本(并更新其旧版本,当前版本为1.14.7)。