Todos os produtos
Search
Central de documentação

Object Storage Service:Gerenciar o Block Public Access de um ponto de acesso com o OSS SDK for Python 2.0

Última atualização: Jul 03, 2026

Este tópico descreve como usar o Object Storage Service (OSS) SDK for Python 2.0 para gerenciar o Block Public Access de um ponto de acesso.

Observações de uso

  • O código de exemplo deste tópico usa o ID da região cn-hangzhou, referente à região China (Hangzhou). Por padrão, o acesso aos recursos de um bucket ocorre via endpoint público. Para acessar esses recursos por meio de outros serviços da Alibaba Cloud na mesma região do bucket, use um endpoint interno. Para mais informações sobre regiões e endpoints, consulte Regiões e endpoints.

Exemplos

Ativar o Block Public Access para um ponto de acesso

O código de exemplo a seguir mostra como ativar o Block Public Access para um ponto de acesso:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to enable Block Public Access for an access point.
parser = argparse.ArgumentParser(description="put access point public access block sample")

# Specify the command line parameters, including the region, bucket name, access point name, and endpoint, and specify whether to enable Block Public Access. The endpoint parameter is optional.
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')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)
parser.add_argument('--block_public_access',
                    help='Specifies whether to enable Block Public Access. '
                         'true: enables Block Public Access. '
                         'false (default): disables Block Public Access.',
                    default='false')

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Obtain access credentials from environment variables for authentication.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Specify the region attribute of the configuration object based on the command line parameters specified by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to specify Block Public Access for an access point.
    result = client.put_access_point_public_access_block(oss.PutAccessPointPublicAccessBlockRequest(
            bucket=args.bucket, # The name of the bucket.
            access_point_name=args.access_point_name, # The name of the access point.
            public_access_block_configuration=oss.PublicAccessBlockConfiguration(
                block_public_access=args.block_public_access == 'true', # Enable Block Public Access.
            ),
    ))

    # Display the HTTP status code and request ID of the request to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in the functions of the script. The control program flow starts here.

Consultar as configurações de Block Public Access de um ponto de acesso

O código de exemplo a seguir mostra como consultar as configurações de Block Public Access de um ponto de acesso:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to query the Block Public Access configurations of an access point.
parser = argparse.ArgumentParser(description="get access point public access block sample")

# Specify the command line parameters, including the region, bucket name, access point name, and endpoint. The endpoint parameter is optional.
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')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Obtain access credentials from environment variables for authentication.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Specify the region attribute of the configuration object based on the command line parameters specified by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to query the Block Public Access configurations of an access point.
    result = client.get_access_point_public_access_block(oss.GetAccessPointPublicAccessBlockRequest(
            bucket=args.bucket, # The name of the bucket.
            access_point_name=args.access_point_name, # The name of the access point.
    ))

    # Display the HTTP status code, request ID, and the Block Public Access status of the request to check the request status and the Block Public Access configurations.
    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")},'
          )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in the functions of the script. The control program flow starts here.

Excluir as configurações de Block Public Access de um ponto de acesso

O código de exemplo a seguir mostra como excluir as configurações de Block Public Access de um ponto de acesso:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line parameter parser and describe the purpose of the script. The example describes how to query the Block Public Access configurations of an access point.
parser = argparse.ArgumentParser(description="delete access point public access block sample")

# Specify the command line parameters, including the region, bucket name, access point name, and endpoint. The endpoint parameter is optional.
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')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)

def main():
    # Parse the command line parameters to obtain the values specified by the user.
    args = parser.parse_args()

    # Obtain access credentials from environment variables for authentication.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK to create a configuration object and specify the credential provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Specify the region attribute of the configuration object based on the command line parameters specified by the user.
    cfg.region = args.region

    # If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to delete the Block Public Access configurations of an access point.
    result = client.delete_access_point_public_access_block(oss.DeleteAccessPointPublicAccessBlockRequest(
            bucket=args.bucket, # The name of the bucket.
            access_point_name=args.access_point_name, # The name of the access point.
    ))

    # Display the HTTP status code and request ID of the request to check the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
    main() # Specify the entry points in the functions of the script. The control program flow starts here.