文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
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
-
- 适用于 Java 的 SDK 2.x
-
使用 AWS 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.logging.Level; import java.util.logging.Logger; public DescribeTimeToLiveResponse describeTTL(final String tableName, final Region region) { final DescribeTimeToLiveRequest request = DescribeTimeToLiveRequest.builder().tableName(tableName).build(); try (DynamoDbClient ddb = dynamoDbClient != null ? dynamoDbClient : DynamoDbClient.builder().region(region).build()) { return ddb.describeTimeToLive(request); } catch (ResourceNotFoundException e) { System.err.format(TABLE_NOT_FOUND_ERROR, tableName); throw e; } catch (DynamoDbException e) { System.err.println(e.getMessage()); throw e; } }
-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考DescribeTimeToLive中的。
-
- JavaScript
-
- 适用于 JavaScript (v3) 的软件开发工具包
-
使用 适用于 JavaScript 的 AWS SDK描述现有 DynamoDB 表上的 TTL 配置。
import { DynamoDBClient, DescribeTimeToLiveCommand } from "@aws-sdk/client-dynamodb"; export const describeTTL = 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; } } // Example usage (commented out for testing) // describeTTL('your-table-name', 'us-east-1');
-
有关 API 的详细信息,请参阅 适用于 JavaScript 的 AWS SDK API 参考DescribeTimeToLive中的。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
使用 适用于 Python (Boto3) 的 AWS SDK描述现有 DynamoDB 表上的 TTL 配置。
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 的详细信息,请参阅适用DescribeTimeToLive于 Python 的AWS SDK (Boto3) API 参考。
-
DescribeTable
ExecuteStatement