Todos os produtos
Search
Central de documentação

Object Storage Service:Lifecycle management (Python SDK V2)

Última atualização: Sep 24, 2026

Este tópico descreve como usar o Python SDK V2 para gerencie as regras de ciclo de vida de um bucket.

Informações gerais

No OSS, nem todos os dados enviados exigem acesso frequente. No entanto, para fins de conformidade ou arquivamento, alguns dados precisam ser armazenados como cold storage. De acordo com seus requisitos de negócio, selecione uma das seguintes opções:

  1. Regras de ciclo de vida baseadas na última modificação: se os dados não foram alterados por um longo período e não precisam mais ser retidos, use esse tipo de regra para exclua-los em lote ou converter sua classe de armazenamento para uma classe de cold storage, liberando espaço.

  2. Regras de ciclo de vida baseadas no último acesso: ao ative esse tipo de regra, o OSS monitora automaticamente os padrões de acesso aos dados, identifica dados que não foram acessados por um longo período e os converte para uma classe de armazenamento mais econômica. Esse processo implementa o Automatic Storage Tiering e reduz os custos de armazenamento.

Precauções

  • Antes de configure uma regra de ciclo de vida baseada na última modificação ou no último acesso, certifique-se de compreender o recurso. Para mais informações, consulte Lifecycle rules based on the last modified time e Lifecycle rules based on the last access time.

  • O código de exemplo neste tópico utiliza a região China (Hangzhou), cujo ID de região é cn-hangzhou. Por padrão, um endpoint público é utilizado. Para acessar o OSS a partir de outros serviços Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre as regiões e endpoints compatíveis com o OSS, consulte OSS regions and endpoints.

  • Para defina uma regra de ciclo de vida, você precisa da permissão oss:PutBucketLifecycle. Para visualize regras de ciclo de vida, é necessária a permissão oss:GetBucketLifecycle. Para exclua todas as regras de ciclo de vida, é necessária a permissão oss:DeleteBucketLifecycle. Para mais informações sobre como executar essas operações, consulte Grant custom permissions to a RAM user.

Defina regras de ciclo de vida

Os exemplos de código a seguir demonstram como defina regras de ciclo de vida baseadas na última modificação e no último acesso. Para modifique uma ou mais regras após defini-las, consulte How do I modify the configurations of one or more lifecycle rules?

Converter a classe de armazenamento de objetos com base em uma política que usa a última modificação

O exemplo de código a seguir demonstra como defina uma regra de ciclo de vida para um bucket, convertendo a classe de armazenamento dos objetos com base na última modificação.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser to receive user-entered parameters.
parser = argparse.ArgumentParser(description="put bucket lifecycle sample")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)

# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
  # Parse command-line arguments.
  args = parser.parse_args()

  # Load credentials (AccessKey ID and AccessKey secret) from environment variables.
  credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

  # Load the default configurations of the SDK.
  cfg = oss.config.load_default()

  # Set the credentials provider.
  cfg.credentials_provider = credentials_provider

  # Set the region where the bucket is located.
  cfg.region = args.region

  # If a custom endpoint is provided by the user, set it in the configuration.
  if args.endpoint is not None:
    cfg.endpoint = args.endpoint

    # Initialize the OSS client using the configuration object.
  client = oss.Client(cfg)

  result = client.put_bucket_lifecycle(oss.PutBucketLifecycleRequest(
    bucket=args.bucket,
    lifecycle_configuration=oss.LifecycleConfiguration(
      rules=[oss.LifecycleRule(
        # Specify lifecycle rule rule1. In this rule, objects that have the prefix foo/ and the tag k1:v1 are converted to the Infrequent Access (IA) storage class 30 days after they are last modified.
        id='rule1',
        status='Enabled',
        prefix='foo/',
        transitions=[oss.LifecycleRuleTransition(
          days=30,
          storage_class=oss.StorageClassType.IA,
          is_access_time=False, # Set to false, which indicates that the policy is based on the last modified time.
        )],
        tags=[oss.Tag(
          key='k1',
          value='v1',
        )],
      ), oss.LifecycleRule(
        # Specify lifecycle rule rule2. In this rule, for objects that have the prefix dir/, delete markers are automatically deleted if the objects are in a versioning-enabled bucket and have only delete markers. Non-current versions of objects expire and are deleted after 30 days. Non-current versions of objects are converted to the IA storage class after 10 days.
        id='rule2',
        status='Enabled',
        prefix='dir/',
        expiration=oss.LifecycleRuleExpiration(
          expired_object_delete_marker=True
        ),
        noncurrent_version_expiration=oss.NoncurrentVersionExpiration(
          noncurrent_days=30,
        ),
        noncurrent_version_transitions=[oss.NoncurrentVersionTransition(
          noncurrent_days=10,
          storage_class=oss.StorageClassType.IA,
          is_access_time=False,
        )],
      )]
    ),
  ))

  # Print the status code and request ID of the operation.
  print(f'status code: {result.status_code}, '  # The HTTP status code, which indicates whether the request is successful.
        f'request id: {result.request_id}')    # The request ID, which is used to track request logs and for debugging.

if __name__ == "__main__":
  # The program entry point that calls the main function to execute the logic.
  main()

Converter a classe de armazenamento de objetos sem um prefixo ou tag específicos com base em uma política que usa a última modificação

O exemplo de código a seguir mostra como converter objetos de um bucket para a classe de armazenamento Infrequent Access 30 dias após a última modificação. Essa regra se aplica a objetos que atendem aos requisitos de tamanho especificados, não possuem o prefixo log e não possuem uma tag com chave key1 e valor value1.

import argparse
import datetime
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser to receive user-entered parameters.
parser = argparse.ArgumentParser(description="put bucket lifecycle sample")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)

# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse command-line arguments.
    args = parser.parse_args()

    # Load credentials (AccessKey ID and AccessKey secret) from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK.
    cfg = oss.config.load_default()

    # Set the credentials provider.
    cfg.credentials_provider = credentials_provider

    # Set the region where the bucket is located.
    cfg.region = args.region

    # If a custom endpoint is provided by the user, set it in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the configuration object.
    client = oss.Client(cfg)

    result = client.put_bucket_lifecycle(oss.PutBucketLifecycleRequest(
            bucket=args.bucket,
            lifecycle_configuration=oss.LifecycleConfiguration(
                rules=[oss.LifecycleRule(
                    # Specify lifecycle rule rule1. In this rule, objects that have the prefix logs/ and a size between 500 bytes and 1,000 bytes are converted to the Infrequent Access (IA) storage class 30 days after they are last modified. This rule does not apply to objects that also have the prefix logs/log and the tag key:key1 and value:value1.
                    id='rule1',
                    status='Enabled',
                    prefix='logs/',
                    transitions=[oss.LifecycleRuleTransition(
                        days=30,
                        storage_class=oss.StorageClassType.IA,
                        is_access_time=False, # Set to false, which indicates that the policy is based on the last modified time.
                    )],
                    filter=oss.LifecycleRuleFilter(
                        object_size_greater_than=500,
                        object_size_less_than=1000,
                        filter_not=[oss.LifecycleRuleNot(
                            prefix='logs/log',
                            tag={
                               'key': 'key1',
                                'value': 'value1',
                            },
                        )],
                    )
                )]
            ),
    ))

    # Print the status code and request ID of the operation.
    print(f'status code: {result.status_code}, '  # The HTTP status code, which indicates whether the request is successful.
          f'request id: {result.request_id}')    # The request ID, which is used to track request logs and for debugging.

if __name__ == "__main__":
    # The program entry point that calls the main function to execute the logic.
    main()

Converter a classe de armazenamento de objetos com base em uma política que usa o último acesso

O exemplo de código a seguir ilustra como defina uma regra de ciclo de vida para um bucket, convertendo a classe de armazenamento dos objetos com base no último acesso.

import argparse
import datetime
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put bucket lifecycle sample")
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():
  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_bucket_lifecycle(oss.PutBucketLifecycleRequest(
    bucket=args.bucket,
    lifecycle_configuration=oss.LifecycleConfiguration(
      rules=[
        oss.LifecycleRule(
          id='rule1',
          status='Enabled',
          prefix='data/',
          transitions=[oss.LifecycleRuleTransition(
            days=200,
            storage_class=oss.StorageClassType.IA,
            is_access_time=True,
            return_to_std_when_visit=False
          )]
        ),
        oss.LifecycleRule(
          id='rule2',
          status='Enabled',
          prefix='log/',
          transitions=[
            oss.LifecycleRuleTransition(
              days=120,
              storage_class=oss.StorageClassType.IA,
              is_access_time=True,
              return_to_std_when_visit=False
            ),
            oss.LifecycleRuleTransition(
              days=250,
              storage_class=oss.StorageClassType.ARCHIVE
              # If is_access_time is not set, the rule is based on the last modified time by default.
            )
          ]
        )
      ]
    )
  ))

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

if __name__ == "__main__":
  main()

Visualize regras de ciclo de vida

O exemplo de código a seguir demonstra como visualize regras de ciclo de vida.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser to receive user-entered parameters.
parser = argparse.ArgumentParser(description="get bucket lifecycle sample")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)

# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse command-line arguments.
    args = parser.parse_args()

    # Load credentials (AccessKey ID and AccessKey secret) from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK.
    cfg = oss.config.load_default()

    # Set the credentials provider.
    cfg.credentials_provider = credentials_provider

    # Set the region where the bucket is located.
    cfg.region = args.region

    # If a custom endpoint is provided by the user, set it in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the configuration object.
    client = oss.Client(cfg)

    # Call the get_bucket_lifecycle method to obtain the lifecycle rules of the bucket.
    result = client.get_bucket_lifecycle(
        oss.GetBucketLifecycleRequest(
            bucket=args.bucket,  # Specify the name of the destination bucket.
        )
    )

    # Print the status code and request ID of the operation.
    print(f'status code: {result.status_code}, '  # The HTTP status code, which indicates whether the request is successful.
          f'request id: {result.request_id}')    # The request ID, which is used to track request logs and for debugging.

    # If the returned collection of lifecycle rules contains rules, print the details of each rule one by one.
    if result.lifecycle_configuration.rules:  # Check whether lifecycle rules exist.
        for r in result.lifecycle_configuration.rules:  # Traverse all rules.
            print(f'rule: {r}')  # Print the content of each rule.

if __name__ == "__main__":
    # The program entry point that calls the main function to execute the logic.
    main()

Exclua todas as regras de ciclo de vida

O exemplo de código a seguir mostra como exclua todas as regras de ciclo de vida de um bucket chamado examplebucket. Para exclua uma ou mais regras de ciclo de vida específicas, consulte How do I delete one or more lifecycle rules?

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser to receive user-entered parameters.
parser = argparse.ArgumentParser(description="delete bucket lifecycle sample")

# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)

# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)

# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse command-line arguments.
    args = parser.parse_args()

    # Load credentials (AccessKey ID and AccessKey secret) from environment variables.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Load the default configurations of the SDK.
    cfg = oss.config.load_default()

    # Set the credentials provider.
    cfg.credentials_provider = credentials_provider

    # Set the region where the bucket is located.
    cfg.region = args.region

    # If a custom endpoint is provided by the user, set it in the configuration.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the configuration object.
    client = oss.Client(cfg)

    # Call the delete_bucket_lifecycle method to delete the lifecycle rules of the bucket.
    result = client.delete_bucket_lifecycle(
        oss.DeleteBucketLifecycleRequest(
            bucket=args.bucket,  # Specify the name of the destination bucket.
        )
    )

    # Print the status code and request ID of the operation.
    print(f'status code: {result.status_code}, '  # The HTTP status code, which indicates whether the request is successful.
          f'request id: {result.request_id}')    # The request ID, which is used to track request logs and for debugging.

if __name__ == "__main__":
    # The program entry point that calls the main function to execute the logic.
    main()

Referências

  • Para obter o código de exemplo completo de definição de regra de ciclo de vida, consulte put_bucket_lifecycle.py.

  • Para acessar o código de exemplo completo de visualização de regras de ciclo de vida, consulte get_bucket_lifecycle.py.

  • Para consultar o código de exemplo completo de exclusão de todas as regras de ciclo de vida, acesse delete_bucket_lifecycle.py.