Skip to content

Commit df627a5

Browse files
authored
V2 (#6)
* Porting the code to make use of the AWS SDK for Java 2.0.
1 parent 07587a7 commit df627a5

16 files changed

+307
-209
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@ You can download release builds through the [releases section of this](https://g
1919
* **Minimum requirements** -- You'll need Java 8 (or later) and [Maven 3](http://maven.apache.org/).
2020
* **Download** -- Download the [latest preview release](https://github.com/awslabs/large-payload-offloading-java-common-lib-for-aws/releases) or pick it up from Maven:
2121

22+
### Version 2.x
23+
```xml
24+
<dependency>
25+
<groupId>software.amazon.payloadoffloading</groupId>
26+
<artifactId>payloadoffloading-common</artifactId>
27+
<version>2.0.0</version>
28+
</dependency>
29+
```
30+
31+
### Version 1.x
2232
```xml
2333
<dependency>
2434
<groupId>software.amazon.payloadoffloading</groupId>
2535
<artifactId>payloadoffloading-common</artifactId>
2636
<version>1.0.0</version>
27-
<type>jar</type>
2837
</dependency>
2938
```
3039

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.amazon.payloadoffloading</groupId>
88
<artifactId>payloadoffloading-common</artifactId>
9-
<version>1.0.0</version>
9+
<version>2.0.0</version>
1010
<packaging>jar</packaging>
1111
<name>Payload offloading common library for AWS</name>
1212
<description>Common library between extended Amazon AWS clients to save payloads up to 2GB on Amazon S3.</description>
@@ -36,15 +36,21 @@
3636
</developers>
3737

3838
<properties>
39-
<aws-java-sdk.version>1.11.817</aws-java-sdk.version>
39+
<aws-java-sdk.version>2.13.64</aws-java-sdk.version>
4040
</properties>
4141

4242
<dependencies>
4343
<dependency>
44-
<groupId>com.amazonaws</groupId>
45-
<artifactId>aws-java-sdk-s3</artifactId>
44+
<groupId>software.amazon.awssdk</groupId>
45+
<artifactId>s3</artifactId>
4646
<version>${aws-java-sdk.version}</version>
4747
</dependency>
48+
<dependency>
49+
<groupId>software.amazon.awssdk</groupId>
50+
<artifactId>utils</artifactId>
51+
<version>${aws-java-sdk.version}</version>
52+
</dependency>
53+
4854
<dependency>
4955
<groupId>junit</groupId>
5056
<artifactId>junit</artifactId>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package software.amazon.payloadoffloading;
2+
3+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
4+
import software.amazon.awssdk.services.s3.model.ServerSideEncryption;
5+
6+
public class AwsManagedCmk implements ServerSideEncryptionStrategy {
7+
@Override
8+
public void decorate(PutObjectRequest.Builder putObjectRequestBuilder) {
9+
putObjectRequestBuilder.serverSideEncryption(ServerSideEncryption.AWS_KMS);
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package software.amazon.payloadoffloading;
2+
3+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
4+
import software.amazon.awssdk.services.s3.model.ServerSideEncryption;
5+
6+
public class CustomerKey implements ServerSideEncryptionStrategy {
7+
private final String awsKmsKeyId;
8+
9+
public CustomerKey(String awsKmsKeyId) {
10+
this.awsKmsKeyId = awsKmsKeyId;
11+
}
12+
13+
@Override
14+
public void decorate(PutObjectRequest.Builder putObjectRequestBuilder) {
15+
putObjectRequestBuilder.serverSideEncryption(ServerSideEncryption.AWS_KMS);
16+
putObjectRequestBuilder.ssekmsKeyId(awsKmsKeyId);
17+
}
18+
}

src/main/java/software/amazon/payloadoffloading/PayloadS3Pointer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package software.amazon.payloadoffloading;
22

3-
import com.amazonaws.AmazonClientException;
4-
import org.apache.commons.logging.Log;
5-
import org.apache.commons.logging.LogFactory;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import software.amazon.awssdk.core.exception.SdkClientException;
66

77
/**
88
* This class is used for carrying pointer to Amazon S3 objects which contain payloads.
99
*/
1010
public class PayloadS3Pointer {
11-
private static final Log LOG = LogFactory.getLog(PayloadS3Pointer.class);
11+
private static final Logger LOG = LoggerFactory.getLogger(PayloadS3Pointer.class);
1212
private String s3BucketName;
1313
private String s3Key;
1414

@@ -38,7 +38,7 @@ public String toJson() {
3838
} catch (Exception e) {
3939
String errorMessage = "Failed to convert S3 object pointer to text.";
4040
LOG.error(errorMessage, e);
41-
throw new AmazonClientException(errorMessage, e);
41+
throw SdkClientException.create(errorMessage, e);
4242
}
4343
return s3PointerStr;
4444
}
@@ -52,7 +52,7 @@ public static PayloadS3Pointer fromJson(String s3PointerJson) {
5252
} catch (Exception e) {
5353
String errorMessage = "Failed to read the S3 object pointer from given string.";
5454
LOG.error(errorMessage, e);
55-
throw new AmazonClientException(errorMessage, e);
55+
throw SdkClientException.create(errorMessage, e);
5656
}
5757
return s3Pointer;
5858
}

src/main/java/software/amazon/payloadoffloading/PayloadStorageConfiguration.java

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
package software.amazon.payloadoffloading;
22

3-
import com.amazonaws.AmazonClientException;
4-
import com.amazonaws.annotation.NotThreadSafe;
5-
import com.amazonaws.services.s3.AmazonS3;
6-
import com.amazonaws.services.s3.model.SSEAwsKeyManagementParams;
7-
import org.apache.commons.logging.Log;
8-
import org.apache.commons.logging.LogFactory;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import software.amazon.awssdk.annotations.NotThreadSafe;
6+
import software.amazon.awssdk.core.exception.SdkClientException;
7+
import software.amazon.awssdk.services.s3.S3Client;
98

109
/**
11-
* Amazon payload storage configuration options such as Amazon S3 client,
12-
* bucket name, and payload size threshold for payloads.
10+
* <p>Amazon payload storage configuration options such as Amazon S3 client,
11+
* bucket name, and payload size threshold for payloads.</p>
12+
*
13+
* <p>Server side encryption is optional and can be enabled using with {@link #withServerSideEncryption(ServerSideEncryptionStrategy)}
14+
* or {@link #setServerSideEncryptionStrategy(ServerSideEncryptionStrategy)}</p>
15+
*
16+
* <p>There are two possible options for server side encrption. This can be using a customer managed key or AWS managed CMK.</p>
17+
*
18+
* Example usage:
19+
*
20+
* <pre>
21+
* withServerSideEncryption(ServerSideEncrptionFactory.awsManagedCmk())
22+
* </pre>
23+
*
24+
* or
25+
*
26+
* <pre>
27+
* withServerSideEncryption(ServerSideEncrptionFactory.customerKey(YOUR_CUSTOMER_ID))
28+
* </pre>
29+
*
30+
* @see software.amazon.payloadoffloading.ServerSideEncryptionFactory
1331
*/
1432
@NotThreadSafe
1533
public class PayloadStorageConfiguration {
16-
private static final Log LOG = LogFactory.getLog(PayloadStorageConfiguration.class);
34+
private static final Logger LOG = LoggerFactory.getLogger(PayloadStorageConfiguration.class);
1735

18-
private AmazonS3 s3;
36+
private S3Client s3;
1937
private String s3BucketName;
2038
private int payloadSizeThreshold = 0;
2139
private boolean alwaysThroughS3 = false;
2240
private boolean payloadSupport = false;
2341
/**
2442
* This field is optional, it is set only when we want to configure S3 Server Side Encryption with KMS.
2543
*/
26-
private SSEAwsKeyManagementParams sseAwsKeyManagementParams;
44+
private ServerSideEncryptionStrategy serverSideEncryptionStrategy;
2745

2846
public PayloadStorageConfiguration() {
2947
s3 = null;
3048
s3BucketName = null;
31-
sseAwsKeyManagementParams = null;
49+
serverSideEncryptionStrategy = null;
3250
}
3351

3452
public PayloadStorageConfiguration(PayloadStorageConfiguration other) {
35-
this.s3 = other.getAmazonS3Client();
53+
this.s3 = other.getS3Client();
3654
this.s3BucketName = other.getS3BucketName();
37-
this.sseAwsKeyManagementParams = other.getSSEAwsKeyManagementParams();
3855
this.payloadSupport = other.isPayloadSupportEnabled();
3956
this.alwaysThroughS3 = other.isAlwaysThroughS3();
4057
this.payloadSizeThreshold = other.getPayloadSizeThreshold();
58+
this.serverSideEncryptionStrategy = other.getServerSideEncryptionStrategy();
4159
}
4260

4361
/**
@@ -47,11 +65,11 @@ public PayloadStorageConfiguration(PayloadStorageConfiguration other) {
4765
* @param s3BucketName Name of the bucket which is going to be used for storing payload.
4866
* The bucket must be already created and configured in s3.
4967
*/
50-
public void setPayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
68+
public void setPayloadSupportEnabled(S3Client s3, String s3BucketName) {
5169
if (s3 == null || s3BucketName == null) {
5270
String errorMessage = "S3 client and/or S3 bucket name cannot be null.";
5371
LOG.error(errorMessage);
54-
throw new AmazonClientException(errorMessage);
72+
throw SdkClientException.create(errorMessage);
5573
}
5674
if (isPayloadSupportEnabled()) {
5775
LOG.warn("Payload support is already enabled. Overwriting AmazonS3Client and S3BucketName.");
@@ -70,7 +88,7 @@ public void setPayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
7088
* The bucket must be already created and configured in s3.
7189
* @return the updated PayloadStorageConfiguration object.
7290
*/
73-
public PayloadStorageConfiguration withPayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
91+
public PayloadStorageConfiguration withPayloadSupportEnabled(S3Client s3, String s3BucketName) {
7492
setPayloadSupportEnabled(s3, s3BucketName);
7593
return this;
7694
}
@@ -109,7 +127,7 @@ public boolean isPayloadSupportEnabled() {
109127
*
110128
* @return Reference to the Amazon S3 client which is being used.
111129
*/
112-
public AmazonS3 getAmazonS3Client() {
130+
public S3Client getS3Client() {
113131
return s3;
114132
}
115133

@@ -122,35 +140,6 @@ public String getS3BucketName() {
122140
return s3BucketName;
123141
}
124142

125-
/**
126-
* Gets the S3 SSE-KMS encryption params of S3 objects under configured S3 bucket name.
127-
*
128-
* @return The S3 SSE-KMS params used for encryption.
129-
*/
130-
public SSEAwsKeyManagementParams getSSEAwsKeyManagementParams() {
131-
return sseAwsKeyManagementParams;
132-
}
133-
134-
/**
135-
* Sets the the S3 SSE-KMS encryption params of S3 objects under configured S3 bucket name.
136-
*
137-
* @param sseAwsKeyManagementParams The S3 SSE-KMS params used for encryption.
138-
*/
139-
public void setSSEAwsKeyManagementParams(SSEAwsKeyManagementParams sseAwsKeyManagementParams) {
140-
this.sseAwsKeyManagementParams = sseAwsKeyManagementParams;
141-
}
142-
143-
/**
144-
* Sets the the S3 SSE-KMS encryption params of S3 objects under configured S3 bucket name.
145-
*
146-
* @param sseAwsKeyManagementParams The S3 SSE-KMS params used for encryption.
147-
* @return the updated PayloadStorageConfiguration object
148-
*/
149-
public PayloadStorageConfiguration withSSEAwsKeyManagementParams(SSEAwsKeyManagementParams sseAwsKeyManagementParams) {
150-
setSSEAwsKeyManagementParams(sseAwsKeyManagementParams);
151-
return this;
152-
}
153-
154143
/**
155144
* Sets the payload size threshold for storing payloads in Amazon S3.
156145
*
@@ -212,4 +201,38 @@ public boolean isAlwaysThroughS3() {
212201
public void setAlwaysThroughS3(boolean alwaysThroughS3) {
213202
this.alwaysThroughS3 = alwaysThroughS3;
214203
}
204+
205+
/**
206+
* Sets which method of server side encryption should be used, if required.
207+
*
208+
* This is optional, it is set only when you want to configure S3 server side encryption with KMS.
209+
*
210+
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
211+
* @return the updated PayloadStorageConfiguration object.
212+
*/
213+
public PayloadStorageConfiguration withServerSideEncryption(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
214+
setServerSideEncryptionStrategy(serverSideEncryptionStrategy);
215+
return this;
216+
}
217+
218+
/**
219+
* Sets which method of server side encryption should be use, if required.
220+
*
221+
* This is optional, it is set only when you want to configure S3 Server Side Encryption with KMS.
222+
*
223+
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
224+
*/
225+
public void setServerSideEncryptionStrategy(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
226+
this.serverSideEncryptionStrategy = serverSideEncryptionStrategy;
227+
}
228+
229+
/**
230+
* The method of service side encryption which should be used, if required.
231+
*
232+
* @return The server side encryption method required. Default null.
233+
*/
234+
public ServerSideEncryptionStrategy getServerSideEncryptionStrategy() {
235+
return this.serverSideEncryptionStrategy;
236+
}
237+
215238
}

src/main/java/software/amazon/payloadoffloading/PayloadStore.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package software.amazon.payloadoffloading;
22

3-
import com.amazonaws.AmazonClientException;
4-
import com.amazonaws.AmazonServiceException;
3+
import software.amazon.awssdk.core.exception.SdkClientException;
4+
import software.amazon.awssdk.services.s3.model.S3Exception;
55

66
/**
77
* An AWS storage service that supports saving high payload sizes.
@@ -11,26 +11,25 @@ public interface PayloadStore {
1111
* Stores payload in a store that has higher payload size limit than that is supported by original payload store.
1212
*
1313
* @param payload
14-
* @param payloadContentSize
1514
* @return a pointer that must be used to retrieve the original payload later.
16-
* @throws AmazonClientException If any internal errors are encountered on the client side while
15+
* @throws SdkClientException If any internal errors are encountered on the client side while
1716
* attempting to make the request or handle the response. For example
1817
* if a network connection is not available.
19-
* @throws AmazonServiceException If an error response is returned by actual PayloadStore indicating
18+
* @throws S3Exception If an error response is returned by actual PayloadStore indicating
2019
* either a problem with the data in the request, or a server side issue.
2120
*/
22-
String storeOriginalPayload(String payload, Long payloadContentSize);
21+
String storeOriginalPayload(String payload);
2322

2423
/**
2524
* Retrieves the original payload using the given payloadPointer. The pointer must
2625
* have been obtained using {@link storeOriginalPayload}
2726
*
2827
* @param payloadPointer
2928
* @return original payload
30-
* @throws AmazonClientException If any internal errors are encountered on the client side while
29+
* @throws SdkClientException If any internal errors are encountered on the client side while
3130
* attempting to make the request or handle the response. For example
3231
* if payloadPointer is invalid or a network connection is not available.
33-
* @throws AmazonServiceException If an error response is returned by actual PayloadStore indicating
32+
* @throws S3Exception If an error response is returned by actual PayloadStore indicating
3433
* a server side issue.
3534
*/
3635
String getOriginalPayload(String payloadPointer);
@@ -40,10 +39,10 @@ public interface PayloadStore {
4039
* have been obtained using {@link storeOriginalPayload}
4140
*
4241
* @param payloadPointer
43-
* @throws AmazonClientException If any internal errors are encountered on the client side while
42+
* @throws SdkClientException If any internal errors are encountered on the client side while
4443
* attempting to make the request or handle the response to/from PayloadStore.
4544
* For example, if payloadPointer is invalid or a network connection is not available.
46-
* @throws AmazonServiceException If an error response is returned by actual PayloadStore indicating
45+
* @throws S3Exception If an error response is returned by actual PayloadStore indicating
4746
* a server side issue.
4847
*/
4948
void deleteOriginalPayload(String payloadPointer);

0 commit comments

Comments
 (0)