-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPayloadStorageConfiguration.java
More file actions
219 lines (197 loc) · 7.65 KB
/
PayloadStorageConfiguration.java
File metadata and controls
219 lines (197 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package software.amazon.payloadoffloading;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.NotThreadSafe;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
/**
* <p>Amazon payload storage configuration options such as synchronous Amazon S3 client,
* bucket name, and payload size threshold for payloads.</p>
*
* <p>Server side encryption is optional and can be enabled using with {@link #withServerSideEncryption(ServerSideEncryptionStrategy)}
* or {@link #setServerSideEncryptionStrategy(ServerSideEncryptionStrategy)}</p>
*
* <p>There are two possible options for server side encrption. This can be using a customer managed key or AWS managed CMK.</p>
*
* Example usage:
*
* <pre>
* withServerSideEncryption(ServerSideEncrptionFactory.awsManagedCmk())
* </pre>
*
* or
*
* <pre>
* withServerSideEncryption(ServerSideEncrptionFactory.customerKey(YOUR_CUSTOMER_ID))
* </pre>
*
* @see software.amazon.payloadoffloading.ServerSideEncryptionFactory
*/
@NotThreadSafe
public class PayloadStorageConfiguration extends PayloadStorageConfigurationBase {
private static final Logger LOG = LoggerFactory.getLogger(PayloadStorageConfiguration.class);
private S3Client s3;
private int streamUploadPartSize = 5 * 1024 * 1024;
private int streamUploadThreshold = 5 * 1024 * 1024;
public PayloadStorageConfiguration() {
s3 = null;
}
public PayloadStorageConfiguration(PayloadStorageConfiguration other) {
super(other);
this.s3 = other.getS3Client();
this.streamUploadThreshold = other.getStreamUploadThreshold();
this.streamUploadPartSize = other.getStreamUploadPartSize();
}
/**
* Enables support for payloads .
*
* @param s3 Amazon S3 client which is going to be used for storing payload.
* @param s3BucketName Name of the bucket which is going to be used for storing payload.
* The bucket must be already created and configured in s3.
*/
public void setPayloadSupportEnabled(S3Client s3, String s3BucketName) {
if (s3 == null || s3BucketName == null) {
String errorMessage = "S3 client and/or S3 bucket name cannot be null.";
LOG.error(errorMessage);
throw SdkClientException.create(errorMessage);
}
super.setPayloadSupportEnabled(s3BucketName);
this.s3 = s3;
}
/**
* Enables support for payload.
*
* @param s3 Amazon S3 client which is going to be used for storing payloads.
* @param s3BucketName Name of the bucket which is going to be used for storing payloads.
* The bucket must be already created and configured in s3.
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withPayloadSupportEnabled(S3Client s3, String s3BucketName) {
setPayloadSupportEnabled(s3, s3BucketName);
return this;
}
/**
* Disables support for payloads.
*/
public void setPayloadSupportDisabled() {
super.setPayloadSupportDisabled();
s3 = null;
}
/**
* Disables support for payload.
*
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withPayloadSupportDisabled() {
setPayloadSupportDisabled();
return this;
}
/**
* Gets the Amazon S3 client which is being used for storing payloads.
*
* @return Reference to the Amazon S3 client which is being used.
*/
public S3Client getS3Client() {
return s3;
}
/**
* Sets the payload size threshold for storing payloads in Amazon S3.
*
* @param payloadSizeThreshold Payload size threshold to be used for storing in Amazon S3.
* Default: 256KB.
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withPayloadSizeThreshold(int payloadSizeThreshold) {
setPayloadSizeThreshold(payloadSizeThreshold);
return this;
}
/**
* Sets whether or not all payloads regardless of their size should be stored in Amazon S3.
*
* @param alwaysThroughS3 Whether or not all payloads regardless of their size
* should be stored in Amazon S3. Default: false
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withAlwaysThroughS3(boolean alwaysThroughS3) {
setAlwaysThroughS3(alwaysThroughS3);
return this;
}
/**
* Sets which method of server side encryption should be used, if required.
*
* This is optional, it is set only when you want to configure S3 server side encryption with KMS.
*
* @param serverSideEncryptionStrategy The method of encryption required for S3 server side encryption with KMS.
* @return the updated PayloadStorageConfiguration object.
*/
public PayloadStorageConfiguration withServerSideEncryption(ServerSideEncryptionStrategy serverSideEncryptionStrategy) {
setServerSideEncryptionStrategy(serverSideEncryptionStrategy);
return this;
}
/**
* Configures the ACL to apply to the Amazon S3 putObject request.
* @param objectCannedACL
* The ACL to be used when storing objects in Amazon S3
*/
public PayloadStorageConfiguration withObjectCannedACL(ObjectCannedACL objectCannedACL) {
setObjectCannedACL(objectCannedACL);
return this;
}
/**
* Enables or disables stream upload support.
* @param enabled true to enable stream uploads when threshold exceeded.
* @return updated configuration
*/
public PayloadStorageConfiguration withStreamUploadEnabled(boolean enabled) {
setStreamUploadEnabled(enabled);
return this;
}
/**
* Sets the stream upload threshold (in bytes). Only used when stream upload is enabled.
* @param threshold threshold in bytes (must be >0) otherwise default (5MB) is applied.
* @return updated configuration
*/
public PayloadStorageConfiguration withStreamUploadThreshold(int threshold) {
setStreamUploadThreshold(threshold);
return this;
}
public PayloadStorageConfiguration withStreamUploadPartSize(int partSize) {
setStreamUploadPartSize(partSize);
return this;
}
/**
* Gets the stream upload threshold in bytes. Default 5MB.
*
* @return threshold in bytes.
*/
public int getStreamUploadThreshold() { return streamUploadThreshold; }
/**
* Sets the stream upload threshold in bytes. Values less than or equal to zero will reset to default (5MB).
*
* @param streamUploadThreshold threshold in bytes
*/
public void setStreamUploadThreshold(int streamUploadThreshold) {
int min = 5 * 1024 * 1024;
if (streamUploadThreshold <= min) {
this.streamUploadThreshold = min;
} else {
this.streamUploadThreshold = streamUploadThreshold;
}
}
/**
* Gets the configured stream upload part size (bytes). Default 5MB.
*/
public int getStreamUploadPartSize() { return streamUploadPartSize; }
/**
* Sets the stream upload part size (bytes). Values < 5MB are rounded up to 5MB.
*/
public void setStreamUploadPartSize(int partSize) {
int min = 5 * 1024 * 1024;
if (partSize < min) {
this.streamUploadPartSize = min;
} else {
this.streamUploadPartSize = partSize;
}
}
}