简体   繁体   中英

Reading aws config and credentials from resources folder using aws java sdk version 2

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.

You should not place credentials files in resources directory. AWS Java SDK supports credential files in ~/.aws out-of-the box:

The following list shows the supported credential retrieval techniques:

  1. Java system propertiesaws.accessKeyId and aws.secretAccessKey . The AWS SDK for Java uses the SystemPropertyCredentialsProvider to load these credentials.

  2. Environment variablesAWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY . The AWS SDK for Java uses the EnvironmentVariableCredentialsProvider class to load these credentials.

  3. The default credential profiles file – The specific location of this file can vary per platform, but is typically located at ~/.aws/credentials . This file is shared by many of the AWS SDKs and by the AWS CLI. The AWS SDK for Java uses the ProfileCredentialsProvider to load these credentials.

    You can create a credentials file by using the aws configure command provided by the AWS CLI. You can also create it by editing the file with a text editor. For information about the credentials file format, see AWS Credentials File Format.

  4. Amazon ECS container credentials – This is loaded from Amazon ECS if the environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set. The AWS SDK for Java uses the ContainerCredentialsProvider to load these credentials.

  5. Instance profile credentials – This is used on Amazon EC2 instances, and delivered through the Amazon EC2 metadata service. The AWS SDK for Java uses the InstanceProfileCredentialsProvider to load these credentials.

So, either use ProfileCredentialsProvider or pass the credentials via system properties or environment variables and use SystemPropertyCredentialsProvider / EnvironmentVariableCredentialsProvider .

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM