Todos os produtos
Search
Central de documentação

Object Storage Service:Access tracking (Python SDK V2)

Última atualização: Jul 03, 2026

Este tópico descreve como usar o Python SDK V2 para configurar o recurso de rastreamento de acesso de um bucket.

Observações

  • 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 sistema usa um endpoint público. Para acessar o OSS a partir de outros produtos da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre os mapeamentos entre regiões e endpoints do OSS, consulte Regiões e endpoints do OSS.

Definição dos métodos

Ativar o rastreamento de acesso

put_bucket_access_monitor(request: PutBucketAccessMonitorRequest, **kwargs) → PutBucketAccessMonitorResult

Consultar o status do rastreamento de acesso

get_bucket_access_monitor(request: GetBucketAccessMonitorRequest, **kwargs) → GetBucketAccessMonitorResult

Parâmetros da solicitação

Parâmetro

Tipo

Descrição

request

PutBucketAccessMonitorRequest

Parâmetros da solicitação. Para obter mais informações, consulte PutBucketAccessMonitorRequest

GetBucketAccessMonitorRequest

Parâmetros da solicitação. Para obter mais informações, consulte GetBucketAccessMonitorRequest

Valores de retorno

Tipo

Descrição

PutBucketAccessMonitorResult

Valor de retorno. Para obter mais informações, consulte PutBucketAccessMonitorResult

GetBucketAccessMonitorResult

Valor de retorno. Para obter mais informações, consulte GetBucketAccessMonitorResult

Para obter a definição completa sobre como ativar o rastreamento de acesso, consulte put_bucket_access_monitor.

Para obter a definição completa sobre como consultar o status do rastreamento de acesso, consulte get_bucket_access_monitor.

Exemplos de código

Ativar o rastreamento de acesso

O código a seguir mostra como ativar o rastreamento de acesso de um bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: configure the access tracking status for a bucket.
parser = argparse.ArgumentParser(description="put bucket access monitor sample")

# Define command-line arguments, including the required region, bucket name, endpoint, and access tracking status.
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('--status', help='The access tracking status of the bucket. Valid values: Enabled, Disabled.', required=True)

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

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

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

    # Initialize the OSS client based on the preceding configurations to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to configure the access tracking status of the specified bucket.
    result = client.put_bucket_access_monitor(oss.PutBucketAccessMonitorRequest(
            bucket=args.bucket,  # The name of the bucket.
            access_monitor_configuration=oss.AccessMonitorConfiguration(
                status=args.status,  # Set the new access tracking status.
            ),
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')

# When this script is directly executed, call the main function to start the processing logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.

Consultar o status do rastreamento de acesso

O código a seguir mostra como consultar o status do rastreamento de acesso de um bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: obtain the access tracking status of a bucket.
parser = argparse.ArgumentParser(description="get bucket access monitor sample")

# Define command-line arguments, including the required region and bucket name, and the optional endpoint.
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():
    # Parse command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default SDK configurations and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command-line arguments provided by the user.
    cfg.region = args.region

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

    # Initialize the OSS client based on the preceding configurations to interact with OSS.
    client = oss.Client(cfg)

    # Execute a request to obtain the access tracking status of the specified bucket.
    result = client.get_bucket_access_monitor(oss.GetBucketAccessMonitorRequest(
            bucket=args.bucket,  # The name of the bucket.
    ))

    # Print the status code, request ID, and access tracking status of the operation result.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' status: {result.access_monitor_configuration.status},'
    )

# When this script is directly executed, call the main function to start the processing logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.

Referências