Skip to content

Commit 208f10c

Browse files
feat: Add async support (#50)
* Add S3AsyncClient support * Update version to 2.2.0 to reflect new feature * Separate sync and async configuration. --------- Co-authored-by: Richard Smith <[email protected]>
1 parent df2156b commit 208f10c

11 files changed

+1013
-134
lines changed

Diff for: pom.xml

+1-1
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>2.1.3</version>
9+
<version>2.2.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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package software.amazon.payloadoffloading;
2+
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.S3AsyncClient;
8+
import software.amazon.awssdk.services.s3.S3Client;
9+
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
10+
11+
/**
12+
* <p>Amazon payload storage configuration options such as asynchronous Amazon S3 client,
13+
* bucket name, and payload size threshold for payloads.</p>
14+
*
15+
* <p>Server side encryption is optional and can be enabled using with {@link #withServerSideEncryption(ServerSideEncryptionStrategy)}
16+
* or {@link #setServerSideEncryptionStrategy(ServerSideEncryptionStrategy)}</p>
17+
*
18+
* <p>There are two possible options for server side encrption. This can be using a customer managed key or AWS managed CMK.</p>
19+
*
20+
* Example usage:
21+
*
22+
* <pre>
23+
* withServerSideEncryption(ServerSideEncrptionFactory.awsManagedCmk())
24+
* </pre>
25+
*
26+
* or
27+
*
28+
* <pre>
29+
* withServerSideEncryption(ServerSideEncrptionFactory.customerKey(YOUR_CUSTOMER_ID))
30+
* </pre>
31+
*
32+
* @see software.amazon.payloadoffloading.ServerSideEncryptionFactory
33+
*/
34+
@NotThreadSafe
35+
public class PayloadStorageAsyncConfiguration extends PayloadStorageConfigurationBase {
36+
private static final Logger LOG = LoggerFactory.getLogger(PayloadStorageAsyncConfiguration.class);
37+
38+
private S3AsyncClient s3Async;
39+
40+
public PayloadStorageAsyncConfiguration() {
41+
s3Async = null;
42+
}
43+
44+
public PayloadStorageAsyncConfiguration(PayloadStorageAsyncConfiguration other) {
45+
super(other);
46+
this.s3Async = other.getS3AsyncClient();
47+
}
48+
49+
/**
50+
* Enables support for payloads using asynchronous storage.
51+
*
52+
* @param s3Async Amazon S3 client which is going to be used for storing payload.
53+
* @param s3BucketName Name of the bucket which is going to be used for storing payload.
54+
* The bucket must be already created and configured in s3.
55+
*/
56+
public void setPayloadSupportEnabled(S3AsyncClient s3Async, String s3BucketName) {
57+
if (s3Async == null || s3BucketName == null) {
58+
String errorMessage = "S3 client and/or S3 bucket name cannot be null.";
59+
LOG.error(errorMessage);
60+
throw SdkClientException.create(errorMessage);
61+
}
62+
super.setPayloadSupportEnabled(s3BucketName);
63+
this.s3Async = s3Async;
64+
}
65+
66+
/**
67+
* Enables support for payload.
68+
*
69+
* @param s3Async Amazon S3 client which is going to be used for storing payload.
70+
* @param s3BucketName Name of the bucket which is going to be used for storing payloads.
71+
* The bucket must be already created and configured in s3.
72+
* @return the updated PayloadStorageAsyncConfiguration object.
73+
*/
74+
public PayloadStorageAsyncConfiguration withPayloadSupportEnabled(S3AsyncClient s3Async, String s3BucketName) {
75+
setPayloadSupportEnabled(s3Async, s3BucketName);
76+
return this;
77+
}
78+
79+
/**
80+
* Disables support for payloads.
81+
*/
82+
public void setPayloadSupportDisabled() {
83+
super.setPayloadSupportDisabled();
84+
s3Async = null;
85+
LOG.info("Payload support disabled.");
86+
}
87+
88+
/**
89+
* Disables support for payload.
90+
*
91+
* @return the updated PayloadStorageAsyncConfiguration object.
92+
*/
93+
public PayloadStorageAsyncConfiguration withPayloadSupportDisabled() {
94+
setPayloadSupportDisabled();
95+
return this;
96+
}
97+
98+
/**
99+
* Gets the Amazon S3 async client which is being used for storing payloads.
100+
*
101+
* @return Reference to the Amazon S3 async client which is being used.
102+
*/
103+
public S3AsyncClient getS3AsyncClient() {
104+
return s3Async;
105+
}
106+
107+
/**
108+
* Sets the payload size threshold for storing payloads in Amazon S3.
109+
*
110+
* @param payloadSizeThreshold Payload size threshold to be used for storing in Amazon S3.
111+
* Default: 256KB.
112+
* @return the updated PayloadStorageAsyncConfiguration object.
113+
*/
114+
public PayloadStorageAsyncConfiguration withPayloadSizeThreshold(int payloadSizeThreshold) {
115+
setPayloadSizeThreshold(payloadSizeThreshold);
116+
return this;
117+
}
118+
119+
/**
120+
* Sets whether or not all payloads regardless of their size should be stored in Amazon S3.
121+
*
122+
* @param alwaysThroughS3 Whether or not all payloads regardless of their size
123+
* should be stored in Amazon S3. Default: false
124+
* @return the updated PayloadStorageAsyncConfiguration object.
125+
*/
126+
public PayloadStorageAsyncConfiguration withAlwaysThroughS3(boolean alwaysThroughS3) {
127+
setAlwaysThroughS3(alwaysThroughS3);
128+
return this;
129+
}
130+
131+
/**
132+
* Sets which method of server side encryption should be used, if required.
133+
*
134+
* This is optional, it is set only when you want to configure S3 server side encryption with KMS.
135+
*
136+
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
137+
* @return the updated PayloadStorageAsyncConfiguration object.
138+
*/
139+
public PayloadStorageAsyncConfiguration withServerSideEncryption(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
140+
setServerSideEncryptionStrategy(serverSideEncryptionStrategy);
141+
return this;
142+
}
143+
144+
/**
145+
* Configures the ACL to apply to the Amazon S3 putObject request.
146+
* @param objectCannedACL
147+
* The ACL to be used when storing objects in Amazon S3
148+
*/
149+
public PayloadStorageAsyncConfiguration withObjectCannedACL(ObjectCannedACL objectCannedACL) {
150+
setObjectCannedACL(objectCannedACL);
151+
return this;
152+
}
153+
}

Diff for: src/main/java/software/amazon/payloadoffloading/PayloadStorageConfiguration.java

+5-133
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
99

1010
/**
11-
* <p>Amazon payload storage configuration options such as Amazon S3 client,
11+
* <p>Amazon payload storage configuration options such as synchronous Amazon S3 client,
1212
* bucket name, and payload size threshold for payloads.</p>
1313
*
1414
* <p>Server side encryption is optional and can be enabled using with {@link #withServerSideEncryption(ServerSideEncryptionStrategy)}
@@ -31,38 +31,18 @@
3131
* @see software.amazon.payloadoffloading.ServerSideEncryptionFactory
3232
*/
3333
@NotThreadSafe
34-
public class PayloadStorageConfiguration {
34+
public class PayloadStorageConfiguration extends PayloadStorageConfigurationBase {
3535
private static final Logger LOG = LoggerFactory.getLogger(PayloadStorageConfiguration.class);
3636

3737
private S3Client s3;
38-
private String s3BucketName;
39-
private int payloadSizeThreshold = 0;
40-
private boolean alwaysThroughS3 = false;
41-
private boolean payloadSupport = false;
42-
/**
43-
* This field is optional, it is set only when we want to configure S3 Server Side Encryption with KMS.
44-
*/
45-
private ServerSideEncryptionStrategy serverSideEncryptionStrategy;
46-
/**
47-
* This field is optional, it is set only when we want to add access control list to Amazon S3 buckets and objects
48-
*/
49-
private ObjectCannedACL objectCannedACL;
5038

5139
public PayloadStorageConfiguration() {
5240
s3 = null;
53-
s3BucketName = null;
54-
serverSideEncryptionStrategy = null;
55-
objectCannedACL = null;
5641
}
5742

5843
public PayloadStorageConfiguration(PayloadStorageConfiguration other) {
44+
super(other);
5945
this.s3 = other.getS3Client();
60-
this.s3BucketName = other.getS3BucketName();
61-
this.payloadSupport = other.isPayloadSupportEnabled();
62-
this.alwaysThroughS3 = other.isAlwaysThroughS3();
63-
this.payloadSizeThreshold = other.getPayloadSizeThreshold();
64-
this.serverSideEncryptionStrategy = other.getServerSideEncryptionStrategy();
65-
this.objectCannedACL = other.getObjectCannedACL();
6646
}
6747

6848
/**
@@ -78,13 +58,8 @@ public void setPayloadSupportEnabled(S3Client s3, String s3BucketName) {
7858
LOG.error(errorMessage);
7959
throw SdkClientException.create(errorMessage);
8060
}
81-
if (isPayloadSupportEnabled()) {
82-
LOG.warn("Payload support is already enabled. Overwriting AmazonS3Client and S3BucketName.");
83-
}
61+
super.setPayloadSupportEnabled(s3BucketName);
8462
this.s3 = s3;
85-
this.s3BucketName = s3BucketName;
86-
this.payloadSupport = true;
87-
LOG.info("Payload support enabled.");
8863
}
8964

9065
/**
@@ -104,10 +79,8 @@ public PayloadStorageConfiguration withPayloadSupportEnabled(S3Client s3, String
10479
* Disables support for payloads.
10580
*/
10681
public void setPayloadSupportDisabled() {
82+
super.setPayloadSupportDisabled();
10783
s3 = null;
108-
s3BucketName = null;
109-
payloadSupport = false;
110-
LOG.info("Payload support disabled.");
11184
}
11285

11386
/**
@@ -120,15 +93,6 @@ public PayloadStorageConfiguration withPayloadSupportDisabled() {
12093
return this;
12194
}
12295

123-
/**
124-
* Check if the support for payloads if enabled.
125-
*
126-
* @return true if support for payloads is enabled.
127-
*/
128-
public boolean isPayloadSupportEnabled() {
129-
return payloadSupport;
130-
}
131-
13296
/**
13397
* Gets the Amazon S3 client which is being used for storing payloads.
13498
*
@@ -138,15 +102,6 @@ public S3Client getS3Client() {
138102
return s3;
139103
}
140104

141-
/**
142-
* Gets the name of the S3 bucket which is being used for storing payload.
143-
*
144-
* @return The name of the bucket which is being used.
145-
*/
146-
public String getS3BucketName() {
147-
return s3BucketName;
148-
}
149-
150105
/**
151106
* Sets the payload size threshold for storing payloads in Amazon S3.
152107
*
@@ -159,25 +114,6 @@ public PayloadStorageConfiguration withPayloadSizeThreshold(int payloadSizeThres
159114
return this;
160115
}
161116

162-
/**
163-
* Gets the payload size threshold for storing payloads in Amazon S3.
164-
*
165-
* @return payload size threshold which is being used for storing in Amazon S3. Default: 256KB.
166-
*/
167-
public int getPayloadSizeThreshold() {
168-
return payloadSizeThreshold;
169-
}
170-
171-
/**
172-
* Sets the payload size threshold for storing payloads in Amazon S3.
173-
*
174-
* @param payloadSizeThreshold Payload size threshold to be used for storing in Amazon S3.
175-
* Default: 256KB.
176-
*/
177-
public void setPayloadSizeThreshold(int payloadSizeThreshold) {
178-
this.payloadSizeThreshold = payloadSizeThreshold;
179-
}
180-
181117
/**
182118
* Sets whether or not all payloads regardless of their size should be stored in Amazon S3.
183119
*
@@ -190,25 +126,6 @@ public PayloadStorageConfiguration withAlwaysThroughS3(boolean alwaysThroughS3)
190126
return this;
191127
}
192128

193-
/**
194-
* Checks whether or not all payloads regardless of their size are being stored in Amazon S3.
195-
*
196-
* @return True if all payloads regardless of their size are being stored in Amazon S3. Default: false
197-
*/
198-
public boolean isAlwaysThroughS3() {
199-
return alwaysThroughS3;
200-
}
201-
202-
/**
203-
* Sets whether or not all payloads regardless of their size should be stored in Amazon S3.
204-
*
205-
* @param alwaysThroughS3 Whether or not all payloads regardless of their size
206-
* should be stored in Amazon S3. Default: false
207-
*/
208-
public void setAlwaysThroughS3(boolean alwaysThroughS3) {
209-
this.alwaysThroughS3 = alwaysThroughS3;
210-
}
211-
212129
/**
213130
* Sets which method of server side encryption should be used, if required.
214131
*
@@ -222,35 +139,6 @@ public PayloadStorageConfiguration withServerSideEncryption(ServerSideEncryption
222139
return this;
223140
}
224141

225-
/**
226-
* Sets which method of server side encryption should be use, if required.
227-
*
228-
* This is optional, it is set only when you want to configure S3 Server Side Encryption with KMS.
229-
*
230-
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
231-
*/
232-
public void setServerSideEncryptionStrategy(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
233-
this.serverSideEncryptionStrategy = serverSideEncryptionStrategy;
234-
}
235-
236-
/**
237-
* The method of service side encryption which should be used, if required.
238-
*
239-
* @return The server side encryption method required. Default null.
240-
*/
241-
public ServerSideEncryptionStrategy getServerSideEncryptionStrategy() {
242-
return this.serverSideEncryptionStrategy;
243-
}
244-
245-
/**
246-
* Configures the ACL to apply to the Amazon S3 putObject request.
247-
* @param objectCannedACL
248-
* The ACL to be used when storing objects in Amazon S3
249-
*/
250-
public void setObjectCannedACL(ObjectCannedACL objectCannedACL) {
251-
this.objectCannedACL = objectCannedACL;
252-
}
253-
254142
/**
255143
* Configures the ACL to apply to the Amazon S3 putObject request.
256144
* @param objectCannedACL
@@ -260,20 +148,4 @@ public PayloadStorageConfiguration withObjectCannedACL(ObjectCannedACL objectCan
260148
setObjectCannedACL(objectCannedACL);
261149
return this;
262150
}
263-
264-
/**
265-
* Checks whether an ACL have been configured for storing objects in Amazon S3.
266-
* @return True if ACL is defined
267-
*/
268-
public boolean isObjectCannedACLDefined() {
269-
return null != objectCannedACL;
270-
}
271-
272-
/**
273-
* Gets the AWS ACL to apply to the Amazon S3 putObject request.
274-
* @return Amazon S3 object ACL
275-
*/
276-
public ObjectCannedACL getObjectCannedACL() {
277-
return objectCannedACL;
278-
}
279151
}

0 commit comments

Comments
 (0)