Java Awssdk Creating and using SystemPropertyCredentialsProvider
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
);
AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(credentials);
- Java Awssdk StaticCredentialsProvider
Using System Properties we can pass "accessKeyId/secretAccessKey" to AWSSDK. These values are provided to the Java virtual machine at the time the virtual machine is started.
java -jar myweb.war -Daws.accessKeyId=my-accessKeyId -Daws.secretAccessKey=my-secretAccessKey
The values of the two properties below will be read by SystemPropertyCredentialsProvider:
- aws.accessKeyId
- aws.secretAccessKey
1. Library
No ADS
pom.xml (*)
<!-- 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. Create SystemPropertyCredentialsProvider
Creating a SystemPropertyCredentialsProvider object is really simple:
SystemPropertyCredentialsProviderUtils.java
package org.o7planning.java_14239_awssdk_s3.utils;
import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider;
public class SystemPropertyCredentialsProviderUtils {
public static SystemPropertyCredentialsProvider create() {
//
// SystemPropertyCredentialsProvider will read "System Properties":
// aws.accessKeyId
// aws.secretAccessKey
//
SystemPropertyCredentialsProvider provider = SystemPropertyCredentialsProvider.create();
return provider;
}
}
Test in Eclipse:
If you are using Eclipse to write code, you can also easily test SystemPropertyCredentialsProvider. See the example below.
SystemPropertyCredentialsProviderTest.java
package org.o7planning.java_14239_awssdk_s3;
import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider;
public class SystemPropertyCredentialsProviderTest {
//
// For Test in Eclipse:
// >> {Right Click}/Run As/Run Configurations../Arguments:
// Add lines below to "VM Arguments":
// -Daws.accessKeyId=my-accessKeyId
// -Daws.secretAccessKey=my-secretAccessKey
//
public static void main(String[] args) {
SystemPropertyCredentialsProvider provider = SystemPropertyCredentialsProvider.create();
System.out.println("provider= " + provider);
System.out.println();
System.out.println("aws.accessKeyId=" + System.getProperty("aws.accessKeyId"));
System.out.println("aws.secretAccessKey=" + System.getProperty("aws.secretAccessKey"));
}
}
- Run Java pass Eclipse System Property
No ADS
Output:
provider= SystemPropertyCredentialsProvider()
aws.accessKeyId=my-accessKeyId
aws.secretAccessKey=my-secretAccessKey
3. Examples
Example, create an S3Client object with SystemPropertyCredentialsProvider:
S3ClientExample.java
package org.o7planning.java_14239_awssdk_s3;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider;
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() {
//
// SystemPropertyCredentialsProvider will read "System Properties":
// aws.accessKeyId
// aws.secretAccessKey
//
AwsCredentialsProvider provider = SystemPropertyCredentialsProvider.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 an S3TransferManager object with SystemPropertyCredentialsProvider:
S3TransferManagerExample.java
package org.o7planning.java_14239_awssdk_s3;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider;
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() {
//
// SystemPropertyCredentialsProvider will read "System Properties":
// aws.accessKeyId
// aws.secretAccessKey
//
AwsCredentialsProvider provider = SystemPropertyCredentialsProvider.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...
}
}
No ADS
Amazon Web Services Tutorials
- Introduction to Amazon Web Services (AWS)
- Introduction to Amazon S3
- Introduction to Amazon Cloudfront and its architecture
- How to reduce Amazon Cloudfront Costs?
- Amazon CloudFront Invalidation
- Introduction to DigitalOcean Spaces
- Create DigitalOcean Spaces Bucket
- Java Awssdk S3 S3Client Upload object
- Create AWS accessKeyId/secretAccessKey
- Java Awssdk S3 List objects
- Host a static website on Amazon S3
- Java Awssdk CloudFront Invalidation
- DigitalOcean Spaces Create Access Key
- Java Awssdk Common Credentials Providers
- Java Awssdk ProfileCredentialsProvider
- Java Awssdk Creating and using EnvironmentVariableCredentialsProvider
- Java Awssdk Creating and using SystemPropertyCredentialsProvider
- Java Awssdk S3 Upload object with S3TransferManager
- Java Awssdk S3 S3TransferManager download object
- Java Manipulate DigitalOcean Spaces using S3TransferManager
- Java Create, list and delete S3 Bucket
- Introduction to Amazon ACM
- Aws Console create IAM User
- Create Amazon S3 Bucket
- Configure custom domain for Amazon S3 static website
- Create a CloudFront distribution for S3 Bucket
- Configure Amazon CloudFront Error Pages
- Amazon S3 Bucket policies
- Amazon AWS Policy Generator - policygen
- Migrate DNS service to Amazon Route 53
- Transfer domain registration to Amazon Route 53
- Request an SSL certificate from Amazon ACM
Show More