将 DescribeTimeToLive 与 AWS SDK 或 CLI 配合使用 - Amazon DynamoDB

DescribeTimeToLive 与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 DescribeTimeToLive

CLI
AWS CLI

查看表的生存时间设置

以下 describe-time-to-live 示例显示 MusicCollection 表的生存时间设置。

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

输出:

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

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的生存时间

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DescribeTimeToLive

Java
SDK for Java 2.x

描述现有 DynamoDB 表中的 TTL 配置。

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);
  • 有关 API 详细信息,请参阅《AWS SDK for Java 2.x API 参考》中的 DescribeTimeToLive

JavaScript
SDK for JavaScript (v3)
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');
  • 有关 API 详细信息,请参阅《AWS SDK for JavaScript API 参考》中的 DescribeTimeToLive

Python
SDK for Python (Boto3)
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')
  • 有关 API 详细信息,请参阅《适用于 Python 的 AWS SDK(Boto3)API 参考》中的 DescribeTimeToLive

有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 结合使用 DynamoDB 与 AWS SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。