Obter a configuração de ciclo de vida de um bucket do Amazon S3 usando um AWS SDK - Exemplos de código do SDK da AWS

Há mais exemplos de AWS SDK disponíveis no repositório AWSDoc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Obter a configuração de ciclo de vida de um bucket do Amazon S3 usando um AWS SDK

O exemplo de código a seguir mostra como obter a configuração de ciclo de vida de um bucket do S3.

.NET
AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

/// <summary> /// Returns a configuration object for the supplied bucket name. /// </summary> /// <param name="client">The S3 client object used to call /// the GetLifecycleConfigurationAsync method.</param> /// <param name="bucketName">The name of the S3 bucket for which a /// configuration will be created.</param> /// <returns>Returns a new LifecycleConfiguration object.</returns> public static async Task<LifecycleConfiguration> RetrieveLifecycleConfigAsync(IAmazonS3 client, string bucketName) { var request = new GetLifecycleConfigurationRequest() { BucketName = bucketName, }; var response = await client.GetLifecycleConfigurationAsync(request); var configuration = response.Configuration; return configuration; }
CLI
AWS CLI

O seguinte comando recupera a configuração do ciclo de vida do bucket my-bucket:

aws s3api get-bucket-lifecycle-configuration --bucket my-bucket

Saída:

{ "Rules": [ { "ID": "Move rotated logs to Glacier", "Prefix": "rotated/", "Status": "Enabled", "Transitions": [ { "Date": "2015-11-10T00:00:00.000Z", "StorageClass": "GLACIER" } ] }, { "Status": "Enabled", "Prefix": "", "NoncurrentVersionTransitions": [ { "NoncurrentDays": 0, "StorageClass": "GLACIER" } ], "ID": "Move old versions to Glacier" } ] }
Python
SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

class BucketWrapper: """Encapsulates S3 bucket actions.""" def __init__(self, bucket): """ :param bucket: A Boto3 Bucket resource. This is a high-level resource in Boto3 that wraps bucket actions in a class-like structure. """ self.bucket = bucket self.name = bucket.name def get_lifecycle_configuration(self): """ Get the lifecycle configuration of the bucket. :return: The lifecycle rules of the specified bucket. """ try: config = self.bucket.LifecycleConfiguration() logger.info( "Got lifecycle rules %s for bucket '%s'.", config.rules, self.bucket.name, ) except: logger.exception( "Couldn't get lifecycle rules for bucket '%s'.", self.bucket.name ) raise else: return config.rules