Skip to content

Check the x-amz-content-range header for GetObject responses when the Content-Range header is not returned by the service. #1861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-AmazonS3-74e484e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "Amazon S3",
"description": "Check the `x-amz-content-range` header for `GetObject` responses when the `Content-Range` header is not returned by the service. Fixes [#1209](https://github.com/aws/aws-sdk-java-v2/issues/1209)."
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void toInputStream() throws Exception {
}
}


@Test
public void toInputStream_loadFromProperties() throws IOException {
s3.putObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY), RequestBody.fromString("test: test"));
Expand Down Expand Up @@ -117,6 +116,13 @@ public void customResponseHandler_InterceptorRecievesResponsePojo() throws Excep
}
}

@Test
public void contentRangeIsReturnedForRangeRequests() {
ResponseInputStream<GetObjectResponse> stream = s3.getObject(getObjectRequest.copy(r -> r.range("bytes=0-1")));
stream.abort();
assertThat(stream.response().contentRange()).isEqualTo("bytes 0-1/10000");
}

private S3Client createClientWithInterceptor(ExecutionInterceptor interceptor) {
return s3ClientBuilder().overrideConfiguration(ClientOverrideConfiguration.builder()
.addExecutionInterceptor(interceptor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.s3.internal.handlers;

import java.util.Optional;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkResponse;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;

/**
* Interceptor for {@link GetObjectRequest} messages.
*/
@SdkInternalApi
public class GetObjectInterceptor implements ExecutionInterceptor {
@Override
public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
SdkResponse response = context.response();
if (!(response instanceof GetObjectResponse)) {
return response;
}

return fixContentRange(response, context.httpResponse());
}

/**
* S3 currently returns content-range in two possible headers: Content-Range or x-amz-content-range based on the x-amz-te
* in the request. This will check the x-amz-content-range if the modeled header (Content-Range) wasn't populated.
*/
private SdkResponse fixContentRange(SdkResponse sdkResponse, SdkHttpResponse httpResponse) {
// Use the modeled content range header, if the service returned it.
GetObjectResponse getObjectResponse = (GetObjectResponse) sdkResponse;
if (getObjectResponse.contentRange() != null) {
return getObjectResponse;
}

// If the service didn't use the modeled content range header, check the x-amz-content-range header.
Optional<String> xAmzContentRange = httpResponse.firstMatchingHeader("x-amz-content-range");
if (!xAmzContentRange.isPresent()) {
return getObjectResponse;
}

return getObjectResponse.copy(r -> r.contentRange(xAmzContentRange.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ software.amazon.awssdk.services.s3.internal.handlers.AsyncChecksumValidationInte
software.amazon.awssdk.services.s3.internal.handlers.SyncChecksumValidationInterceptor
software.amazon.awssdk.services.s3.internal.handlers.EnableTrailingChecksumInterceptor
software.amazon.awssdk.services.s3.internal.handlers.ExceptionTranslationInterceptor
software.amazon.awssdk.services.s3.internal.handlers.GetObjectInterceptor