Skip to content

Commit

Permalink
S3 upload with unknown content-length (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK authored May 25, 2023
1 parent 9ec1e12 commit 12d907e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crt/aws-c-common
2 changes: 1 addition & 1 deletion crt/aws-lc
55 changes: 55 additions & 0 deletions src/test/java/software/amazon/awssdk/crt/test/S3ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,61 @@ public void onFinished(S3FinishedResponseContext context) {
}
}

// Test that we can upload without provide the content length
@Test
public void testS3PutUnknownContentLength() throws IOException {
skipIfNetworkUnavailable();
Assume.assumeTrue(hasAwsCredentials());

Path uploadFilePath = Files.createTempFile("testS3PutFilePath", ".txt");

S3ClientOptions clientOptions = new S3ClientOptions().withRegion(REGION);
try (S3Client client = createS3Client(clientOptions)) {
CompletableFuture<Integer> onFinishedFuture = new CompletableFuture<>();
S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

@Override
public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long objectRangeEnd) {
Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Body Response: " + bodyBytesIn.toString());
return 0;
}

@Override
public void onFinished(S3FinishedResponseContext context) {
Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3,
"Meta request finished with error code " + context.getErrorCode());
if (context.getErrorCode() != 0) {
onFinishedFuture.completeExceptionally(
new CrtS3RuntimeException(context.getErrorCode(), context.getResponseStatus(), context.getErrorPayload()));
return;
}
onFinishedFuture.complete(Integer.valueOf(context.getErrorCode()));
}
};

Files.write(uploadFilePath, createTestPayload(10 * 1024 * 1024));

HttpHeader[] headers = {
new HttpHeader("Host", ENDPOINT),
};
HttpRequest httpRequest = new HttpRequest("PUT", "/put_object_test_filepath_10MB.txt", headers, null);

S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions()
.withMetaRequestType(MetaRequestType.PUT_OBJECT)
.withHttpRequest(httpRequest)
.withRequestFilePath(uploadFilePath)
.withResponseHandler(responseHandler);

try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) {
Assert.assertEquals(Integer.valueOf(0), onFinishedFuture.get());
}
} catch (InterruptedException | ExecutionException ex) {
Assert.fail(ex.getMessage());
} finally {
Files.deleteIfExists(uploadFilePath);
}
}

// Test that passing a nonexistent file path will cause an error
@Test
public void testS3PutNonexistentFilePath() throws IOException {
Expand Down

0 comments on commit 12d907e

Please sign in to comment.