AWS amazondynamodb documentation change
Summary
Updated Java code examples for TTL operations with improved structure, error handling, and documentation comments. Added input validation, constants for configuration, and standardized error messages.
Security assessment
Changes focus on code quality improvements and documentation clarity rather than addressing security vulnerabilities. While TTL helps with data lifecycle management, the changes don't introduce new security features or address specific security flaws.
Diff
diff --git a/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.md b/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.md index a70c2b9fc..a1e205be2 100644 --- a//amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.md +++ b//amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.md @@ -36,0 +37,6 @@ Java + import java.util.HashMap; + import java.util.Map; + import java.util.Optional; + + import com.amazon.samplelib.CodeSampleUtils; + @@ -44,5 +49,0 @@ Java - import software.amazon.awssdk.utils.ImmutableMap; - - import java.io.Serializable; - import java.util.Map; - import java.util.Optional; @@ -49,0 +51,4 @@ Java + /** + * Creates an item in a DynamoDB table with TTL attributes. + * This class demonstrates how to add TTL expiration timestamps to DynamoDB items. + */ @@ -51,2 +56,2 @@ Java - public static void main(String[] args) { - final String usage = """ + + private static final String USAGE = """ @@ -61,3 +66,38 @@ Java - // Optional "region" parameter - if args list length is NOT 3 or 4, short-circuit exit. - if (!(args.length == 3 || args.length == 4)) { - System.out.println(usage); + private static final int DAYS_TO_EXPIRE = 90; + private static final int SECONDS_PER_DAY = 24 * 60 * 60; + private static final String PRIMARY_KEY_ATTR = "primaryKey"; + private static final String SORT_KEY_ATTR = "sortKey"; + private static final String CREATION_DATE_ATTR = "creationDate"; + private static final String EXPIRE_AT_ATTR = "expireAt"; + private static final String SUCCESS_MESSAGE = "%s PutItem operation with TTL successful."; + private static final String TABLE_NOT_FOUND_ERROR = "Error: The Amazon DynamoDB table \"%s\" can't be found."; + + private final DynamoDbClient dynamoDbClient; + + /** + * Constructs a CreateTTL instance with the specified DynamoDB client. + * + * @param dynamoDbClient The DynamoDB client to use + */ + public CreateTTL(final DynamoDbClient dynamoDbClient) { + this.dynamoDbClient = dynamoDbClient; + } + + /** + * Constructs a CreateTTL with a default DynamoDB client. + */ + public CreateTTL() { + this.dynamoDbClient = null; + } + + /** + * Main method to demonstrate creating an item with TTL. + * + * @param args Command line arguments + */ + public static void main(final String[] args) { + try { + int result = new CreateTTL().processArgs(args); + System.exit(result); + } catch (Exception e) { + System.err.println(e.getMessage()); @@ -65,0 +106 @@ Java + } @@ -67,4 +108,29 @@ Java - String tableName = args[0]; - String primaryKey = args[1]; - String sortKey = args[2]; - Region region = Optional.ofNullable(args[3]).isEmpty() ? Region.US_EAST_1 : Region.of(args[3]); + /** + * Process command line arguments and create an item with TTL. + * + * @param args Command line arguments + * @return 0 if successful, non-zero otherwise + * @throws ResourceNotFoundException If the table doesn't exist + * @throws DynamoDbException If an error occurs during the operation + * @throws IllegalArgumentException If arguments are invalid + */ + public int processArgs(final String[] args) { + // Argument validation (remove or replace this line when reusing this code) + CodeSampleUtils.validateArgs(args, new int[]{3, 4}, USAGE); + + final String tableName = args[0]; + final String primaryKey = args[1]; + final String sortKey = args[2]; + final Region region = Optional.ofNullable(args.length > 3 ? args[3] : null) + .map(Region::of) + .orElse(Region.US_EAST_1); + + try (DynamoDbClient ddb = dynamoDbClient != null ? dynamoDbClient : + DynamoDbClient.builder().region(region).build()) { + final CreateTTL createTTL = new CreateTTL(ddb); + createTTL.createItemWithTTL(tableName, primaryKey, sortKey); + return 0; + } catch (Exception e) { + throw e; + } + } @@ -71,0 +138,13 @@ Java + /** + * Creates an item in the specified table with TTL attributes. + * + * @param tableName The name of the table + * @param primaryKeyValue The value for the primary key + * @param sortKeyValue The value for the sort key + * @return The response from the PutItem operation + * @throws ResourceNotFoundException If the table doesn't exist + * @throws DynamoDbException If an error occurs during the operation + */ + public PutItemResponse createItemWithTTL(final String tableName, + final String primaryKeyValue, + final String sortKeyValue) { @@ -76 +155,7 @@ Java - final long expireDate = createDate + (90 * 24 * 60 * 60); + final long expireDate = createDate + (DAYS_TO_EXPIRE * SECONDS_PER_DAY); + + final Map<String, AttributeValue> itemMap = new HashMap<>(); + itemMap.put(PRIMARY_KEY_ATTR, AttributeValue.builder().s(primaryKeyValue).build()); + itemMap.put(SORT_KEY_ATTR, AttributeValue.builder().s(sortKeyValue).build()); + itemMap.put(CREATION_DATE_ATTR, AttributeValue.builder().n(String.valueOf(createDate)).build()); + itemMap.put(EXPIRE_AT_ATTR, AttributeValue.builder().n(String.valueOf(expireDate)).build()); @@ -78,5 +162,0 @@ Java - final ImmutableMap<String, ? extends Serializable> itemMap = - ImmutableMap.of("primaryKey", primaryKey, - "sortKey", sortKey, - "creationDate", createDate, - "expireAt", expireDate); @@ -85 +165 @@ Java - .item((Map<String, AttributeValue>) itemMap) + .item(itemMap) @@ -87,6 +167,5 @@ Java - try (DynamoDbClient ddb = DynamoDbClient.builder() - .region(region) - .build()) { - final PutItemResponse response = ddb.putItem(request); - System.out.println(tableName + " PutItem operation with TTL successful. Request id is " - + response.responseMetadata().requestId()); + + try { + final PutItemResponse response = dynamoDbClient.putItem(request); + System.out.println(String.format(SUCCESS_MESSAGE, tableName)); + return response; @@ -94,2 +173,2 @@ Java - System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName); - System.exit(1); + System.err.format(TABLE_NOT_FOUND_ERROR, tableName); + throw e; @@ -98 +177 @@ Java - System.exit(1); + throw e; @@ -100 +178,0 @@ Java - System.exit(0); @@ -242,0 +321,4 @@ Update TTL on an existing DynamoDB item in a table. + import java.util.HashMap; + import java.util.Map; + import java.util.Optional; + @@ -250,4 +331,0 @@ Update TTL on an existing DynamoDB item in a table. - import software.amazon.awssdk.utils.ImmutableMap; - - import java.util.Map; - import java.util.Optional; @@ -254,0 +333,3 @@ Update TTL on an existing DynamoDB item in a table. + public UpdateItemResponse updateItemWithTTL(final String tableName, + final String primaryKeyValue, + final String sortKeyValue) { @@ -258,11 +340,11 @@ Update TTL on an existing DynamoDB item in a table. - final long expireDate = currentTime + (90 * 24 * 60 * 60); - // An expression that defines one or more attributes to be updated, the action to be performed on them, and new values for them. - final String updateExpression = "SET updatedAt=:c, expireAt=:e"; - - final ImmutableMap<String, AttributeValue> keyMap = - ImmutableMap.of("primaryKey", AttributeValue.fromS(primaryKey), - "sortKey", AttributeValue.fromS(sortKey)); - final Map<String, AttributeValue> expressionAttributeValues = ImmutableMap.of( - ":c", AttributeValue.builder().s(String.valueOf(currentTime)).build(), - ":e", AttributeValue.builder().s(String.valueOf(expireDate)).build() - ); + final long expireDate = currentTime + (DAYS_TO_EXPIRE * SECONDS_PER_DAY); + + // Create the key map for the item to update + final Map<String, AttributeValue> keyMap = new HashMap<>(); + keyMap.put(PRIMARY_KEY_ATTR, AttributeValue.builder().s(primaryKeyValue).build()); + keyMap.put(SORT_KEY_ATTR, AttributeValue.builder().s(sortKeyValue).build()); + + // Create the expression attribute values + final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); + expressionAttributeValues.put(":c", AttributeValue.builder().n(String.valueOf(currentTime)).build()); + expressionAttributeValues.put(":e", AttributeValue.builder().n(String.valueOf(expireDate)).build()); @@ -273 +355 @@ Update TTL on an existing DynamoDB item in a table. - .updateExpression(updateExpression) + .updateExpression(UPDATE_EXPRESSION) @@ -276,6 +358,5 @@ Update TTL on an existing DynamoDB item in a table.