o7planning

Java Awssdk Creating and using EnvironmentVariableCredentialsProvider

  1. Library
  2. EnvironmentVariableCredentialsProvider
  3. Examples
As you know "accessKeyId/secretAccessKey" can be hardcoded in Java code, which makes maintenance difficult and presents a security risk.
StaticCredentialsProvider Example
AwsCredentials credentials = AwsBasicCredentials.create( //
		MyAccessKey.ACCESS_KEY_ID, // accessKeyId
		MyAccessKey.SECRET_ACCESS_KEY // secretAccessKey
);
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(credentials);
  • Java Awssdk StaticCredentialsProvider
EnvironmentVariableCredentialsProvider can read "accessKeyId/secretAccessKey" values set in environment variables. These environment variables include:
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_SESSION_TOKEN (Optional)
Image illustrating environment variables on Windows operating system.
Setting environment variables is slightly different on different operating systems. Here's how to set environment variables on a few common operating systems:

1. Library

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/s3 -->
<dependency>
	<groupId>software.amazon.awssdk</groupId>
	<artifactId>s3</artifactId>
	<version>2.21.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/s3-transfer-manager -->
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3-transfer-manager</artifactId>
    <version>2.21.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk.crt/aws-crt -->
<dependency>
    <groupId>software.amazon.awssdk.crt</groupId>
    <artifactId>aws-crt</artifactId>
    <version>0.28.0</version>
</dependency>

<!-- Log Library -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.9</version>
</dependency>

<!-- Log Library -->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.9</version> 
</dependency>  

2. EnvironmentVariableCredentialsProvider

Create EnvironmentVariableCredentialsProvider object:
EnvironmentVariableCredentialsProviderUtils.java
package org.o7planning.java_14237_awssdk_s3.utils;

import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;

public class EnvironmentVariableCredentialsProviderUtils {

	public static EnvironmentVariableCredentialsProvider create() {
		//
		// EnvironmentVariableCredentialsProvider will read Environment Variables:
		// AWS_ACCESS_KEY_ID
		// AWS_SECRET_ACCESS_KEY
		// AWS_SESSION_TOKEN (Optional)
		//
		EnvironmentVariableCredentialsProvider provider = EnvironmentVariableCredentialsProvider.create();

		return provider;
	}
}
AWS_SESSION_TOKEN (Optional)
  • Wait for update...

3. Examples

Example, create S3Client object with EnvironmentVariableCredentialsProvider:
S3ClientExample.java
package org.o7planning.java_14237_awssdk_s3;

import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

public class S3ClientExample {

	// EU (Frankfurt) - Germany.
	private static final Region MY_S3_REGION = Region.EU_CENTRAL_1;

	private static S3Client createS3Client() {
		//
		// EnvironmentVariableCredentialsProvider will read Environment Variables:
		// AWS_ACCESS_KEY_ID
		// AWS_SECRET_ACCESS_KEY
		// AWS_SESSION_TOKEN (Optional)
		//
		EnvironmentVariableCredentialsProvider provider = EnvironmentVariableCredentialsProvider.create();

		S3Client client = S3Client.builder() //
				.credentialsProvider(provider) //
				.region(MY_S3_REGION) //
				.build();

		return client;
	}

	public static void main(String[] args) {
		S3Client s3Client = createS3Client();
		System.out.println("s3Client: " + s3Client);
		// And use s3Client to upload files...
	}
}
  • Java Awssdk S3 Tải file xuống từ S3 Bucket với S3Client
Example, create S3TransferManager object with EnvironmentVariableCredentialsProvider:
S3TransferManager.java
package org.o7planning.java_14237_awssdk_s3;

import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.SizeConstant;

public class S3TransferManagerExample {

	// EU (Frankfurt) - Germany.
	private static final Region MY_S3_REGION = Region.EU_CENTRAL_1;

	private static S3TransferManager createS3TransferManager() {
		//
		// EnvironmentVariableCredentialsProvider will read Environment Variables:
		// AWS_ACCESS_KEY_ID
		// AWS_SECRET_ACCESS_KEY
		// AWS_SESSION_TOKEN (Optional)
		//
		EnvironmentVariableCredentialsProvider provider = EnvironmentVariableCredentialsProvider.create();

		S3AsyncClient s3AsyncClient = S3AsyncClient.crtBuilder() //
				.credentialsProvider(provider) //
				.region(MY_S3_REGION) //
				.targetThroughputInGbps(20.0) //
				.minimumPartSizeInBytes(10 * SizeConstant.MB)//
				.build();

		return S3TransferManager.builder().s3Client(s3AsyncClient).build();
	}

	public static void main(String[] args) {
		S3TransferManager s3TransferManager = createS3TransferManager();
		System.out.println("s3TransferManager: " + s3TransferManager);
		// And use s3TransferManager to upload file,directory...
	}
}