Use OSS SDK for Python V2 to enable, query, and delete the Block Public Access configuration at the bucket level.
All examples use alibabacloud_oss_v2 and load credentials from environment variables. The sample region is China (Hangzhou) (cn-hangzhou). If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For region-to-endpoint mappings, see OSS regions and endpoints.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
Access credentials configured as environment variables
Get the Block Public Access configuration of a bucket
Call get_bucket_public_access_block to retrieve the current Block Public Access configuration. The response includes block_public_access from result.public_access_block_configuration: True if Block Public Access is enabled, False if disabled, or "Not set" if no configuration exists.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="get bucket public access block sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.get_bucket_public_access_block(oss.GetBucketPublicAccessBlockRequest(
bucket=args.bucket,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' block public access: {getattr(result.public_access_block_configuration, "block_public_access", "Not set")},'
)
if __name__ == "__main__":
main()Delete the Block Public Access configuration of a bucket
Call delete_bucket_public_access_block to remove the Block Public Access configuration from a bucket.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="delete bucket public access block sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
args = parser.parse_args()
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
result = client.delete_bucket_public_access_block(oss.DeleteBucketPublicAccessBlockRequest(
bucket=args.bucket,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()