Skip to content
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

S3 upload with unknown content-length #628

Merged
merged 3 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion crt/aws-lc
56 changes: 56 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,62 @@ 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()));
}
};

int contentLength = 10 * 1024 * 1024;
TingDaoK marked this conversation as resolved.
Show resolved Hide resolved
Files.write(uploadFilePath, createTestPayload(contentLength));

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