Usar DescribeTimeToLive com o AWS SDK ou a CLI - Amazon DynamoDB

Usar DescribeTimeToLive com o AWS SDK ou a CLI

Os exemplos de código a seguir mostram como usar o DescribeTimeToLive.

CLI
AWS CLI

Como ver as configurações de vida útil de uma tabela

O exemplo describe-time-to-live a seguir exibe as configurações de vida útil da tabela MusicCollection.

aws dynamodb describe-time-to-live \ --table-name MusicCollection

Saída:

{ "TimeToLiveDescription": { "TimeToLiveStatus": "ENABLED", "AttributeName": "ttl" } }

Para obter mais informações, consulte Vida útil no Guia do desenvolvedor do Amazon DynamoDB.

  • Para obter detalhes da API, consulte DescribeTimeToLive na Referência de comandos da AWS CLI.

Java
SDK para Java 2.x

Descreva a configuração de TTL em uma tabela existente do DynamoDB.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DescribeTimeToLiveRequest; import software.amazon.awssdk.services.dynamodb.model.DescribeTimeToLiveResponse; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; import java.util.Optional; final DescribeTimeToLiveRequest request = DescribeTimeToLiveRequest.builder() .tableName(tableName) .build(); try (DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build()) { final DescribeTimeToLiveResponse response = ddb.describeTimeToLive(request); System.out.println(tableName + " description of time to live is " + response.toString()); } catch (ResourceNotFoundException e) { System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.exit(0);
  • Para obter detalhes da API, consulte DescribeTimeToLive na Referência de API do AWS SDK for Java 2.x.

JavaScript
SDK para JavaScript (v3)
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { DynamoDBClient, DescribeTimeToLiveCommand } from "@aws-sdk/client-dynamodb"; const describeTableTTL = async (tableName, region) => { const client = new DynamoDBClient({ region: region, endpoint: `https://dynamodb.${region}.amazonaws.com` }); try { const ttlDescription = await client.send(new DescribeTimeToLiveCommand({ TableName: tableName })); if (ttlDescription.TimeToLiveDescription.TimeToLiveStatus === 'ENABLED') { console.log("TTL is enabled for table %s.", tableName); } else { console.log("TTL is not enabled for table %s.", tableName); } return ttlDescription; } catch (e) { console.error(`Error describing table: ${e}`); throw e; } } // enter table name and change region if desired. describeTableTTL('your-table-name', 'us-east-1');
  • Para obter detalhes da API, consulte DescribeTimeToLive na Referência de API do AWS SDK for JavaScript.

Python
SDK para Python (Boto3)
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import boto3 def describe_ttl(table_name, region): """ Describes TTL on an existing table, as well as a region. :param table_name: String representing the name of the table :param region: AWS Region of the table - example `us-east-1` :return: Time to live description. """ try: dynamodb = boto3.resource('dynamodb', region_name=region) ttl_description = dynamodb.describe_time_to_live(TableName=table_name) print( f"TimeToLive for table {table_name} is status {ttl_description['TimeToLiveDescription']['TimeToLiveStatus']}") return ttl_description except Exception as e: print(f"Error describing table: {e}") raise # Enter your own table name and AWS region describe_ttl('your-table-name', 'us-east-1')
  • Para obter detalhes da API, consulte DescribeTimeToLive na Referência de API do AWS SDK para Python (Boto3).

Para ver uma lista completa dos Guias do desenvolvedor de SDK da AWS e exemplos de código, consulte Usar o DynamoDB com um AWS SDK. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.