All Products
Search
Document Center

Object Storage Service:Block Public Access for an access point (Python SDK V2)

Last Updated:Mar 20, 2026

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)

Enable Block Public Access for an access point

The following example enables Block Public Access for an access point.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put 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')
parser.add_argument('--block_public_access', default='false')

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.put_access_point_public_access_block(oss.PutAccessPointPublicAccessBlockRequest(
        bucket=args.bucket,
        access_point_name=args.access_point_name,
        public_access_block_configuration=oss.PublicAccessBlockConfiguration(
            block_public_access=args.block_public_access == 'true',
        ),
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')

if __name__ == "__main__":
    main()

Parameters

ParameterRequiredDescription
--regionYesThe region where the bucket is located. Example: cn-hangzhou
--bucketYesThe name of the bucket
--access_point_nameYesThe name of the access point
--endpointNoA custom endpoint. Omit to use the default public endpoint
--block_public_accessNoSet to true to enable Block Public Access, or false to disable it. Default: false

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

ParameterRequiredDescription
--regionYesThe region where the bucket is located. Example: cn-hangzhou
--bucketYesThe name of the bucket
--access_point_nameYesThe name of the access point
--endpointNoA 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

ParameterRequiredDescription
--regionYesThe region where the bucket is located. Example: cn-hangzhou
--bucketYesThe name of the bucket
--access_point_nameYesThe name of the access point
--endpointNoA custom endpoint. Omit to use the default public endpoint