AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above. demonstrates how to get the value of a secret from AWS Secrets Manager.

/* * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package aws.example.secretsmanager; import java.nio.ByteBuffer; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.AmazonWebServiceRequest; import*; import*; public class GetSecretValue { public static void main(String[] args) { getSecret(); } public static void getSecret() { String secretName = "testSecret"; String endpoint = ""; String region = "us-west-2"; AwsClientBuilder.EndpointConfiguration config = new AwsClientBuilder.EndpointConfiguration(endpoint, region); AWSSecretsManagerClientBuilder clientBuilder = AWSSecretsManagerClientBuilder.standard(); clientBuilder.setEndpointConfiguration(config); AWSSecretsManager client =; String secret; ByteBuffer binarySecretData; GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest() .withSecretId(secretName).withVersionStage("AWSCURRENT"); GetSecretValueResult getSecretValueResult = null; try { getSecretValueResult = client.getSecretValue(getSecretValueRequest); } catch(ResourceNotFoundException e) { System.out.println("The requested secret " + secretName + " was not found"); } catch (InvalidRequestException e) { System.out.println("The request was invalid due to: " + e.getMessage()); } catch (InvalidParameterException e) { System.out.println("The request had invalid params: " + e.getMessage()); } if(getSecretValueResult == null) { return; } // Depending on whether the secret was a string or binary, one of these fields will be populated if(getSecretValueResult.getSecretString() != null) { secret = getSecretValueResult.getSecretString(); System.out.println(secret); } else { binarySecretData = getSecretValueResult.getSecretBinary(); System.out.println(binarySecretData.toString()); } } }

Sample Details

Service: secretsmanager

Author: Walker

Type: full-example

On this page: