openplanning

Java Awssdk Tạo và sử dụng SystemPropertyCredentialsProvider

  1. Thư viện
  2. Tạo SystemPropertyCredentialsProvider
  3. Ví dụ
Như bạn đã biết "accessKeyId/secretAccessKey" có thể được code cứng trong mã Java, điều này gây khó khăn cho việc bảo trì và mang tới một rủi ro bảo mật.
StaticCredentialsProvider Example
AwsCredentials credentials = AwsBasicCredentials.create( //
		MyAccessKey.ACCESS_KEY_ID, // accessKeyId
		MyAccessKey.SECRET_ACCESS_KEY // secretAccessKey
);
AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(credentials);
  • Java Awssdk StaticCredentialsProvider
Sử dụng System Properties chúng ta có thể truyền "accessKeyId/secretAccessKey" cho AWSSDK. Các giá trị này được cung cấp cho máy ảo Java tại thời điểm máy ảo được khởi động.
java -jar myweb.war -Daws.accessKeyId=my-accessKeyId -Daws.secretAccessKey=my-secretAccessKey
Giá trị của hai thuộc tính dưới đây sẽ được đọc bởi SystemPropertyCredentialsProvider:
  • aws.accessKeyId
  • aws.secretAccessKey

1. Thư viện

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. Tạo SystemPropertyCredentialsProvider

Tạo đối tượng SystemPropertyCredentialsProvider thực sự rất đơn giản:
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:
Nếu bạn đang sử dụng Eclipse để viết code, bạn cũng có thể dễ dàng test SystemPropertyCredentialsProvider. Hãy xem ví dụ dưới đây.
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
Output:
provider= SystemPropertyCredentialsProvider()

aws.accessKeyId=my-accessKeyId
aws.secretAccessKey=my-secretAccessKey

3. Ví dụ

Ví dụ, tạo đối tượng S3Client với 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
Ví dụ, Tạo đối tượng S3TransferManager với 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...
	}
}

Các hướng dẫn Amazon Web Services

Show More