-
Notifications
You must be signed in to change notification settings - Fork 912
Fix for S3Client with httpClient as url-connection-client fails with EOFException when executing HeadObjectRequest for gzip encoded object #3346
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.changes/next-release/bugfix-URLConnectionHTTPClient-1acc5e0.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "URL Connection HTTP Client", | ||
"contributor": "", | ||
"description": "Fix for S3Client with URL Connection http client fails with EOFException when executing HeadObjectRequest for gzip encodeFix to S3Client fails with EOFException." | ||
} |
64 changes: 64 additions & 0 deletions
64
...ient/src/it/java/software/amazon/awssdk/http/urlconnection/HeadObjectIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.http.urlconnection; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.zip.GZIPOutputStream; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.model.HeadObjectResponse; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
public class HeadObjectIntegrationTest extends UrlHttpConnectionS3IntegrationTestBase { | ||
private static final String BUCKET = temporaryBucketName(HeadObjectIntegrationTest.class); | ||
|
||
private static final String GZIPPED_KEY = "some-key"; | ||
|
||
@BeforeClass | ||
public static void setupFixture() throws IOException { | ||
createBucket(BUCKET); | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
GZIPOutputStream gzos = new GZIPOutputStream(baos); | ||
gzos.write("Test".getBytes(StandardCharsets.UTF_8)); | ||
|
||
s3.putObject(PutObjectRequest.builder() | ||
.bucket(BUCKET) | ||
.key(GZIPPED_KEY) | ||
.contentEncoding("gzip") | ||
.build(), | ||
RequestBody.fromBytes(baos.toByteArray())); | ||
} | ||
|
||
@Test | ||
public void syncClientSupportsGzippedObjects() { | ||
HeadObjectResponse response = s3.headObject(r -> r.bucket(BUCKET).key(GZIPPED_KEY)); | ||
assertThat(response.contentEncoding()).isEqualTo("gzip"); | ||
} | ||
|
||
@AfterClass | ||
public static void cleanup() { | ||
deleteBucketAndAllContents(BUCKET); | ||
} | ||
|
||
} |
142 changes: 142 additions & 0 deletions
142
...ava/software/amazon/awssdk/http/urlconnection/UrlHttpConnectionS3IntegrationTestBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* 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.http.urlconnection; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import org.junit.BeforeClass; | ||
import software.amazon.awssdk.core.ClientType; | ||
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.apache.ApacheHttpClient; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3AsyncClient; | ||
import software.amazon.awssdk.services.s3.S3AsyncClientBuilder; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.S3ClientBuilder; | ||
import software.amazon.awssdk.services.s3.model.BucketLocationConstraint; | ||
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; | ||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest; | ||
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.ListObjectVersionsRequest; | ||
import software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse; | ||
import software.amazon.awssdk.services.s3.model.ListObjectsRequest; | ||
import software.amazon.awssdk.services.s3.model.ListObjectsResponse; | ||
import software.amazon.awssdk.services.s3.model.NoSuchBucketException; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
import software.amazon.awssdk.services.s3.model.S3Object; | ||
import software.amazon.awssdk.testutils.Waiter; | ||
import software.amazon.awssdk.testutils.service.AwsTestBase; | ||
|
||
/** | ||
* Base class for S3 integration tests. Loads AWS credentials from a properties | ||
* file and creates an S3 client for callers to use. | ||
*/ | ||
public class UrlHttpConnectionS3IntegrationTestBase extends AwsTestBase { | ||
|
||
protected static final Region DEFAULT_REGION = Region.US_WEST_2; | ||
/** | ||
* The S3 client for all tests to use. | ||
*/ | ||
protected static S3Client s3; | ||
|
||
/** | ||
* Loads the AWS account info for the integration tests and creates an S3 | ||
* client for tests to use. | ||
*/ | ||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
s3 = s3ClientBuilder().build(); | ||
} | ||
|
||
protected static S3ClientBuilder s3ClientBuilder() { | ||
return S3Client.builder() | ||
.httpClient(UrlConnectionHttpClient.create()) | ||
.region(DEFAULT_REGION) | ||
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN); | ||
|
||
} | ||
|
||
|
||
protected static void createBucket(String bucket) { | ||
Waiter.run(() -> s3.createBucket(r -> r.bucket(bucket))) | ||
.ignoringException(NoSuchBucketException.class) | ||
.orFail(); | ||
} | ||
|
||
protected static void deleteBucketAndAllContents(String bucketName) { | ||
deleteBucketAndAllContents(s3, bucketName); | ||
} | ||
|
||
|
||
public static void deleteBucketAndAllContents(S3Client s3, String bucketName) { | ||
try { | ||
System.out.println("Deleting S3 bucket: " + bucketName); | ||
ListObjectsResponse response = Waiter.run(() -> s3.listObjects(r -> r.bucket(bucketName))) | ||
.ignoringException(NoSuchBucketException.class) | ||
.orFail(); | ||
List<S3Object> objectListing = response.contents(); | ||
|
||
if (objectListing != null) { | ||
while (true) { | ||
for (Iterator<?> iterator = objectListing.iterator(); iterator.hasNext(); ) { | ||
S3Object objectSummary = (S3Object) iterator.next(); | ||
s3.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(objectSummary.key()).build()); | ||
} | ||
|
||
if (response.isTruncated()) { | ||
objectListing = s3.listObjects(ListObjectsRequest.builder() | ||
.bucket(bucketName) | ||
.marker(response.marker()) | ||
.build()) | ||
.contents(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
ListObjectVersionsResponse versions = s3 | ||
.listObjectVersions(ListObjectVersionsRequest.builder().bucket(bucketName).build()); | ||
|
||
if (versions.deleteMarkers() != null) { | ||
versions.deleteMarkers().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder() | ||
.versionId(v.versionId()) | ||
.bucket(bucketName) | ||
.key(v.key()) | ||
.build())); | ||
} | ||
|
||
if (versions.versions() != null) { | ||
versions.versions().forEach(v -> s3.deleteObject(DeleteObjectRequest.builder() | ||
.versionId(v.versionId()) | ||
.bucket(bucketName) | ||
.key(v.key()) | ||
.build())); | ||
} | ||
|
||
s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build()); | ||
} catch (Exception e) { | ||
System.err.println("Failed to delete bucket: " + bucketName); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible that we might need to check for no-content in tryGetErrorStream?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be possible, but will do some deep dive , also will need to add a test case for this . Since I am going on leave will close this PR for now and will raise a separate PR when back from leave , will keep this task is in fast track
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per the code walk we donot need this check for erroStreams , details in #3363