Secrets Manager examples using SDK for Java 2.x - AWS SDK for Java

You can now use the Amazon S3 Transfer Manager (Developer Preview) in the AWS SDK for Java 2.x for accelerated file transfers. Give it a try and let us know what you think! By the way, the AWS SDK for Java team is hiring software development engineers!

Secrets Manager examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Java 2.x with Secrets Manager.

Actions are code excerpts that show you how to call individual Secrets Manager functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Secrets Manager functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to create a Secrets Manager secret.

SDK for Java 2.x

public static String createNewSecret( SecretsManagerClient secretsClient, String secretName, String secretValue) { try { CreateSecretRequest secretRequest = CreateSecretRequest.builder() .name(secretName) .description("This secret was created by the AWS Secret Manager Java API") .secretString(secretValue) .build(); CreateSecretResponse secretResponse = secretsClient.createSecret(secretRequest); return secretResponse.arn(); } catch (SecretsManagerException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
  • Find instructions and more code on GitHub.

  • For API details, see CreateSecret in AWS SDK for Java 2.x API Reference.

The following code example shows how to delete a Secrets Manager secret.

SDK for Java 2.x

public static void deleteSpecificSecret(SecretsManagerClient secretsClient, String secretName) { try { DeleteSecretRequest secretRequest = DeleteSecretRequest.builder() .secretId(secretName) .build(); secretsClient.deleteSecret(secretRequest); System.out.println(secretName +" is deleted."); } catch (SecretsManagerException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see DeleteSecret in AWS SDK for Java 2.x API Reference.

The following code example shows how to describe a Secrets Manager secret.

SDK for Java 2.x

public static void describeGivenSecret(SecretsManagerClient secretsClient, String secretName) { try { DescribeSecretRequest secretRequest = DescribeSecretRequest.builder() .secretId(secretName) .build(); DescribeSecretResponse secretResponse = secretsClient.describeSecret(secretRequest); Instant lastChangedDate = secretResponse.lastChangedDate(); // Convert the Instant to readable date. DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ) .withLocale( Locale.US) .withZone( ZoneId.systemDefault() ); formatter.format( lastChangedDate ); System.out.println("The date of the last change to "+ secretResponse.name() +" is " + lastChangedDate ); } catch (SecretsManagerException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see DescribeSecret in AWS SDK for Java 2.x API Reference.

The following code example shows how to get a Secrets Manager secret value.

SDK for Java 2.x

public static void getValue(SecretsManagerClient secretsClient,String secretName) { try { GetSecretValueRequest valueRequest = GetSecretValueRequest.builder() .secretId(secretName) .build(); GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest); String secret = valueResponse.secretString(); System.out.println(secret); } catch (SecretsManagerException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see GetSecretValue in AWS SDK for Java 2.x API Reference.

The following code example shows how to list Secrets Manager secrets.

SDK for Java 2.x

public static void listAllSecrets(SecretsManagerClient secretsClient) { try { ListSecretsResponse secretsResponse = secretsClient.listSecrets(); List<SecretListEntry> secrets = secretsResponse.secretList(); for (SecretListEntry secret: secrets) { System.out.println("The secret name is "+secret.name()); System.out.println("The secret descreiption is "+secret.description()); } } catch (SecretsManagerException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see ListSecrets in AWS SDK for Java 2.x API Reference.

The following code example shows how to put a value in a Secrets Manager secret.

SDK for Java 2.x

public static void updateMySecret(SecretsManagerClient secretsClient, String secretName, String secretValue) { try { UpdateSecretRequest secretRequest = UpdateSecretRequest.builder() .secretId(secretName) .secretString(secretValue) .build(); secretsClient.updateSecret(secretRequest); } catch (SecretsManagerException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • Find instructions and more code on GitHub.

  • For API details, see PutSecretValue in AWS SDK for Java 2.x API Reference.