Use OSS SDK for Python 2.0 to enable, query, or delete Block Public Access settings for an access point.
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou). Replace it with your actual region.By default, samples connect via a public endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For supported regions and endpoints, see Regions and endpoints.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with an access point configured
Credentials stored in environment variables for authentication
OSS SDK for Python 2.0 installed (
pip install alibabacloud_oss_v2)
Get Block Public Access settings for an access point
The following example retrieves the current Block Public Access settings for an access point.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="get access point public access block sample")
parser.add_argument('--region', required=True)
parser.add_argument('--bucket', required=True)
parser.add_argument('--access_point_name', required=True)
parser.add_argument('--endpoint')
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_access_point_public_access_block(oss.GetAccessPointPublicAccessBlockRequest(
bucket=args.bucket,
access_point_name=args.access_point_name,
))
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()Parameters
| Parameter | Required | Description |
|---|---|---|
--region | Yes | The region where the bucket is located. Example: cn-hangzhou |
--bucket | Yes | The name of the bucket |
--access_point_name | Yes | The name of the access point |
--endpoint | No | A custom endpoint. Omit to use the default public endpoint |
The response includes block_public_access, which shows whether Block Public Access is currently enabled for the access point.
Delete Block Public Access settings for an access point
The following example deletes the Block Public Access settings for an access point.
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="delete access point public access block sample")
parser.add_argument('--region', required=True)
parser.add_argument('--bucket', required=True)
parser.add_argument('--access_point_name', required=True)
parser.add_argument('--endpoint')
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_access_point_public_access_block(oss.DeleteAccessPointPublicAccessBlockRequest(
bucket=args.bucket,
access_point_name=args.access_point_name,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id}')
if __name__ == "__main__":
main()Parameters
| Parameter | Required | Description |
|---|---|---|
--region | Yes | The region where the bucket is located. Example: cn-hangzhou |
--bucket | Yes | The name of the bucket |
--access_point_name | Yes | The name of the access point |
--endpoint | No | A custom endpoint. Omit to use the default public endpoint |