8000 Add an option to disable chunked encoding. · sdatgit/aws-sdk-java@c99559b · GitHub
[go: up one dir, main page]

Skip to content

Commit c99559b

Browse files
author
Harshavardhana
committed
Add an option to disable chunked encoding.
This is done to allow sha256 calculation of proper payload in PutObjectRequest and UploadPartRequest. Fixes aws#580
1 parent b332832 commit c99559b

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/AmazonS3Client.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,7 +2993,8 @@ protected Signer createSigner(final Request<?> request,
29932993
final AmazonWebServiceRequest req = request.getOriginalRequest();
29942994

29952995
if (!(signer instanceof AWSS3V4Signer) && ((upgradeToSigV4(req)))) {
2996-
final AWSS3V4Signer v4Signer = new AWSS3V4Signer();
2996+
boolean isChunkedEncodingDisabled = this.clientOptions.isChunkedEncodingDisabled();
2997+
final AWSS3V4Signer v4Signer = new AWSS3V4Signer(isChunkedEncodingDisabled);
29972998
// Always set the service name; if the user has overridden it via
29982999
// setEndpoint(String, String, String), this will return the right
29993000
// value. Otherwise it will return "s3", which is an appropriate
@@ -3609,8 +3610,13 @@ private <X, Y extends AmazonWebServiceRequest> X invoke(Request<Y> request,
36093610
checkHttps(originalRequest);
36103611
ExecutionContext executionContext = createExecutionContext(originalRequest);
36113612
// Retry V4 auth errors
3612-
executionContext.setAuthErrorRetryStrategy(new S3V4AuthErrorRetryStrategy(buildDefaultEndpointResolver(
3613-
getProtocol(request), bucket, key)));
3613+
S3RequestEndpointResolver endpointResolver = buildDefaultEndpointResolver(getProtocol(request),
3614+
bucket, key);
3615+
boolean isChunkedEncodingDisabled = this.clientOptions.isChunkedEncodingDisabled();
3616+
S3V4AuthErrorRetryStrategy authErrRetryStrategy = new S3V4AuthErrorRetryStrategy(endpointResolver,
3617+
isChunkedEncodingDisabled);
3618+
executionContext.setAuthErrorRetryStrategy(authErrRetryStrategy);
3619+
// -- ends retry strategy.
36143620
AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics();
36153621
// Binds the request metrics to the current request.
36163622
request.setAWSRequestMetrics(awsRequestMetrics);

aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/S3ClientOptions.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ public class S3ClientOptions {
2222
/** The default setting for use of path-style access */
2323
public static final boolean DEFAULT_PATH_STYLE_ACCESS = false;
2424

25+
/** The default setting for use of chunked encoding */
26+
public static final boolean DEFAULT_CHUNKED_ENCODING_DISABLED = false;
27+
2528
/** Flag for use of path-style access */
2629
private boolean pathStyleAccess = DEFAULT_PATH_STYLE_ACCESS;
2730

31+
/** Flag for user of chunked encoding */
32+
private boolean chunkedEncodingDisabled = DEFAULT_CHUNKED_ENCODING_DISABLED;
33+
2834
public S3ClientOptions() {}
2935

3036
public S3ClientOptions( S3ClientOptions other ) {
3137
this.pathStyleAccess = other.pathStyleAccess;
38+
this.chunkedEncodingDisabled = other.chunkedEncodingDisabled;
3239
}
3340

3441
/**
@@ -53,6 +60,28 @@ public boolean isPathStyleAccess() {
5360
return pathStyleAccess;
5461
}
5562

63+
/**
64+
* <p>
65+
* Returns whether the client has chunked encoding disabled for all
66+
* requests.
67+
* </p>
68+
* <p>
69+
* The default behaviour is to detect API request which enables chunked
70+
* encoding automatically for PutObjectRequest and UploadPartRequest.
71+
* Setting this flag will result in disabling chunked encoding for all
72+
* requests.
73+
* Note: Enabling this option has performance implications since each
74+
* payload will have to be checksum calculated. If your payload is large
75+
* this will affect the overall time required to upload an object.
76+
* Using this option is recommended only if your endpoint does not
77+
* implement chunked uploading.
78+
* </p>
79+
* @return True is the client should always enable chunked encoding.
80+
*/
81+
public boolean isChunkedEncodingDisabled() {
82+
return chunkedEncodingDisabled;
83+
}
84+
5685
/**
5786
* <p>
5887
* Configures the client to use path-style access for all requests.
@@ -103,4 +132,27 @@ public S3ClientOptions withPathStyleAccess(boolean pathStyleAccess) {
103132
return this;
104133
}
105134

106-
}
135+
/**
136+
* <p>
137+
* Configures the client to disable chunked encoding for all requests.
138+
* </p>
139+
* <p>
140+
* The default behaviour is to detect API request which enables chunked
141+
* encoding automatically for PutObjectRequest and UploadPartRequest.
142+
* Setting this flag will result in disabling chunked encoding for all
143+
* requests.
144+
* Note: Enabling this option has performance implications since each
145+
* payload will have to be checksum calculated. If your payload is large
146+
* this will affect the overall time required to upload an object.
147+
* Using this option is recommended only if your endpoint does not
148+
* implement chunked uploading.
149+
* </p>
150+
* @return The updated S3ClientOptions object with chunked encoding
151+
* disabled setting.
152+
*/
153+
public S3ClientOptions disableChunkedEncoding() {
154+
this.chunkedEncodingDisabled = true;
155+
return this;
156+
}
157+
158+
}

aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/internal/AWSS3V4Signer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636
*/
3737
public class AWSS3V4Signer extends AWS4Signer {
3838
private static final String CONTENT_SHA_256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD";
39+
private static boolean isChunkedEncodingDisabled;
3940

4041
/**
4142
* Don't double-url-encode path elements; S3 expects path elements to be
4243
* encoded only once in the canonical URI.
4344
*/
44-
public AWSS3V4Signer() {
45+
public AWSS3V4Signer(boolean isChunkedEncodingDisabled) {
4546
super(false);
47+
this.isChunkedEncodingDisabled = isChunkedEncodingDisabled;
4648
}
4749

4850
/**
@@ -120,6 +122,11 @@ protected String calculateContentHash(SignableRequest<?> request) {
120122
private static boolean useChunkEncoding(SignableRequest<?> request) {
121123
// Whether to use chunked encoding for signing the request
122124
boolean chunkedEncodingEnabled = false;
125+
// if chunked encoding is explicitly disabled through client options
126+
// return right here.
127+
if (isChunkedEncodingDisabled) {
128+
return chunkedEncodingEnabled;
129+
}
123130
if (request.getOriginalRequestObject() instanceof PutObjectRequest
124131
|| request.getOriginalRequestObject() instanceof UploadPartRequest) {
125132
chunkedEncodingEnabled = true;

aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/internal/S3V4AuthErrorRetryStrategy.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,25 @@ public class S3V4AuthErrorRetryStrategy implements AuthErrorRetryStrategy {
4646
+ "buckets located in regions that require V4 signing.";
4747

4848
private final S3RequestEndpointResolver endpointResolver;
49+
private final boolean isChunkedEncodingDisabled;
4950
private final SdkPredicate<AmazonServiceException> sigV4RetryPredicate;
5051

51-
public S3V4AuthErrorRetryStrategy(S3RequestEndpointResolver endpointResolver) {
52+
public S3V4AuthErrorRetryStrategy(S3RequestEndpointResolver endpointResolver,
53+
boolean isChunkedEncodingDisabled) {
5254
this.endpointResolver = endpointResolver;
55+
this.isChunkedEncodingDisabled = isChunkedEncodingDisabled;
5356
this.sigV4RetryPredicate = new IsSigV4RetryablePredicate();
5457
}
5558

5659
/**
5760
* Currently only used for testing
5861
*/
5962
S3V4AuthErrorRetryStrategy(S3RequestEndpointResolver endpointResolver,
60-
SdkPredicate<AmazonServiceException> isSigV4Retryable) {
63+
SdkPredicate<AmazonServiceException> isSigV4Retryable,
64+
boolean isChunkedEncodingDisabled) {
6165
this.endpointResolver = endpointResolver;
6266
this.sigV4RetryPredicate = isSigV4Retryable;
67+
this.isChunkedEncodingDisabled = isChunkedEncodingDisabled;
6368
}
6469

6570
@Override
@@ -92,7 +97,7 @@ private AuthRetryParameters redirectToRegionInHeader(Request<?> request, HttpRes
9297
/**
9398
* If the response doesn't have the x-amz-region header we have to resort to sending a request
9499
* to s3-external-1
95-
*
100+
*
96101
* @return
97102
*/
98103
private AuthRetryParameters redirectToS3External() {
@@ -107,7 +112,7 @@ private AuthRetryParameters redirectToS3External() {
107112
}
108113

109114
private AWSS3V4Signer buildSigV4Signer(final String region) {
110-
AWSS3V4Signer v4Signer = new AWSS3V4Signer();
115+
AWSS3V4Signer v4Signer = new AWSS3V4Signer(isChunkedEncodingDisabled);
111116
v4Signer.setRegionName(region);
112117
v4Signer.setServiceName(AmazonS3Client.S3_SERVICE_NAME);
113118
return v4Signer;

0 commit comments

Comments
 (0)
0