Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer le blocage de l'accès public pour un point d'accès à l'aide d'OSS SDK for Python 2.0

Dernière mise à jour :Sep 22, 2026

Cette rubrique explique comment utiliser Object Storage Service (OSS) SDK for Python 2.0 pour gérer le blocage de l'accès public pour un point d'accès.

Notes d'utilisation

  • L'exemple de code de cette rubrique utilise l'ID de région cn-hangzhou de la région Chine (Hangzhou). Par défaut, un endpoint public permet d'accéder aux ressources d'un bucket. Pour accéder aux ressources du bucket depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints, consultez Régions et endpoints.

Exemples

Activer le blocage de l'accès public pour un point d'accès

L'exemple de code suivant montre comment activer le blocage de l'accès public pour un point d'accès :

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.

Interroger les configurations de blocage de l'accès public d'un point d'accès

L'exemple de code suivant montre comment interroger les configurations de blocage de l'accès public d'un point d'accès :

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.

Supprimer les configurations de blocage de l'accès public d'un point d'accès

L'exemple de code suivant montre comment supprimer les configurations de blocage de l'accès public d'un point d'accès :

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.