AWS Java client using different CredentialsProvider
Credentials to a AWS Java client can be passed in multiple ways. Let's see a few of the common ways of building Java client sourcing Credentials from different sources. In this Blog building a Lambda Client is demonstrated, however the same way any Client can be built (such as AmazonDynamoDB etc.)
- Credentials from ~/.aws/credentials file. Refer to this Blog for setting up Credentials
AWSLambda awsLambda = AWSLambdaClientBuilder.defaultClient();
- Credentials from ~/.aws/credentials file using Profile name
String profileName = "lambdaUser";
AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider(profileName);
AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).build();
- Credentials from a properties file in the Classpath. By default AwsCredentials.properties file from Classpath is loaded, this can be overridden.
// Create a CredentilsProvider object using the AwsCredentials.properties in the classpath
AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();
// Build the Client using CredentialsProvider Object
AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).build();
- Credentials passed directly in the Program
// Access and Secret keys of User
String accessKey = "<YOUR_ACCESS_KEY>";
String secretKey = "<YOUR_SECRET_KEY>";
// Build a Credential object
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
// Create a CredentilsProvider object using the Credential object
AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
// Build the Client using CredentialsProvider Object
AWSLambda awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider)
Complete source code can be checked out from the Git Repo
No comments:
Post a Comment