Skip to content

Commit 472f5bf

Browse files
committed
Fix bug in ChecksumCalculatingInputStream
1 parent 2e4a698 commit 472f5bf

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "Amazon S3",
3+
"type": "bugfix",
4+
"description": "S3 putObject API using UrlConnectionHttpClient goes into infinite loop. See https://github.com/aws/aws-sdk-java-v2/pull/942 for more details."
5+
}

http-clients/url-connection-client/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@
5555
<artifactId>junit</artifactId>
5656
<scope>test</scope>
5757
</dependency>
58+
<dependency>
59+
<groupId>software.amazon.awssdk</groupId>
60+
<artifactId>s3</artifactId>
61+
<version>${awsjavasdk.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<artifactId>service-test-utils</artifactId>
66+
<groupId>software.amazon.awssdk</groupId>
67+
<version>${awsjavasdk.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.assertj</groupId>
72+
<artifactId>assertj-core</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>software.amazon.awssdk</groupId>
77+
<artifactId>sdk-core</artifactId>
78+
<version>${awsjavasdk.version}</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>software.amazon.awssdk</groupId>
83+
<artifactId>regions</artifactId>
84+
<version>${awsjavasdk.version}</version>
85+
<scope>test</scope>
86+
</dependency>
5887
</dependencies>
5988

6089
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.urlconnection;
17+
18+
import static software.amazon.awssdk.testutils.service.AwsTestBase.CREDENTIALS_PROVIDER_CHAIN;
19+
20+
import org.assertj.core.api.Assertions;
21+
import org.junit.AfterClass;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
import software.amazon.awssdk.core.sync.RequestBody;
25+
import software.amazon.awssdk.regions.Region;
26+
import software.amazon.awssdk.services.s3.S3Client;
27+
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
28+
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
29+
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
30+
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
31+
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
32+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
33+
34+
public class S3WithUrlHttpClientIntegrationTest {
35+
36+
/**
37+
* The name of the bucket created, used, and deleted by these tests.
38+
*/
39+
private static String BUCKET_NAME = "java-sdk-integ-" + System.currentTimeMillis();
40+
41+
private static String KEY = "key";
42+
43+
private static Region REGION = Region.US_WEST_2;
44+
45+
private static S3Client s3;
46+
47+
/**
48+
* Creates all the test resources for the tests.
49+
*/
50+
@BeforeClass
51+
public static void createResources() throws Exception {
52+
s3 = S3Client.builder()
53+
.region(REGION)
54+
.httpClient(UrlConnectionHttpClient.builder().build())
55+
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
56+
.build();
57+
58+
createBucket(BUCKET_NAME, REGION);
59+
}
60+
61+
/**
62+
* Releases all resources created in this test.
63+
*/
64+
@AfterClass
65+
public static void tearDown() {
66+
deleteObject(BUCKET_NAME, KEY);
67+
deleteBucket(BUCKET_NAME);
68+
}
69+
70+
@Test
71+
public void verifyPutObject() {
72+
Assertions.assertThat(objectCount(BUCKET_NAME)).isEqualTo(0);
73+
74+
// Put Object
75+
s3.putObject(PutObjectRequest.builder().bucket(BUCKET_NAME).key(KEY).build(),
76+
RequestBody.fromString("foobar"));
77+
78+
79+
Assertions.assertThat(objectCount(BUCKET_NAME)).isEqualTo(1);
80+
}
81+
82+
83+
private static void createBucket(String bucket, Region region) {
84+
s3.createBucket(CreateBucketRequest
85+
.builder()
86+
.bucket(bucket)
87+
.createBucketConfiguration(
88+
CreateBucketConfiguration.builder()
89+
.locationConstraint(region.id())
90+
.build())
91+
.build());
92+
}
93+
94+
private static void deleteObject(String bucket, String key) {
95+
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build();
96+
s3.deleteObject(deleteObjectRequest);
97+
}
98+
99+
private static void deleteBucket(String bucket) {
100+
DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
101+
s3.deleteBucket(deleteBucketRequest);
102+
}
103+
104+
private int objectCount(String bucket) {
105+
ListObjectsV2Request listReq = ListObjectsV2Request.builder()
106+
.bucket(bucket)
107+
.build();
108+
109+
return s3.listObjectsV2(listReq).keyCount();
110+
}
111+
}

services/s3/src/main/java/software/amazon/awssdk/services/s3/checksums/ChecksumCalculatingInputStream.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public int read(byte[] buf, int off, int len) throws IOException {
7878
throw new NullPointerException();
7979
}
8080

81-
int read = 0;
81+
int read = -1;
8282

8383
if (!endOfStream) {
8484
read = inputStream.read(buf, off, len);
@@ -89,7 +89,6 @@ public int read(byte[] buf, int off, int len) throws IOException {
8989

9090
if (read == -1) {
9191
endOfStream = true;
92-
read = 0;
9392
}
9493
}
9594

0 commit comments

Comments
 (0)