I have moved my aws credentials from ~/.aws/credentials to resources folder of maven project . the folder structure looks like this resources/aws/ ->config ->credentials I am using aws java sdk version 2+ . How can i read the values from resources folder to get region, access keys , create bucket and perform operations.
Reading aws config and credentials from resources folder using aws java sdk version 2
2.6k views Asked by Adil At
2
There are 2 answers
0

AWS Java SDK v2 does not support getting credentials from the resource folder (classpath) directly.
As an alternative, you can put AWS credentials in a properties file in the resource folder:
[[project]/src/test/resources/aws-credentials.properties:
aws_access_key_id = xxx
aws_secret_access_key = xxx
Spring config:
<util:properties id="awsCredentialFile"
location="classpath:aws-credentials.properties"/>
and your code:
@Resource(name = "awsCredentialFile")
public void setProperties(Properties properties) {
this.accessKey = properties.getProperty("aws_access_key_id");
this.secretKey = properties.getProperty("aws_secret_access_key");
}
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey));
S3Client s3 = S3Client.builder()
.credentialsProvider(credentialsProvider)
.build();
You should not place credentials files in resources directory. AWS Java SDK supports credential files in
~/.aws
out-of-the box:So, either use
ProfileCredentialsProvider
or pass the credentials via system properties or environment variables and useSystemPropertyCredentialsProvider
/EnvironmentVariableCredentialsProvider
.