Skip to content

Commit

Permalink
Do not apply ObjectMetadata#contentLength for UploadPartRequest (#…
Browse files Browse the repository at this point in the history
…1249)

Fixes #1248
  • Loading branch information
Mobe91 authored Oct 2, 2024
1 parent 8020ef1 commit 27eaa94
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,6 @@ void apply(CreateMultipartUploadRequest.Builder builder) {
}

void apply(UploadPartRequest.Builder builder) {
if (contentLength != null) {
builder.contentLength(contentLength);
}
if (sseCustomerAlgorithm != null) {
builder.sseCustomerAlgorithm(sseCustomerAlgorithm);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import software.amazon.awssdk.services.s3.model.RequestPayer;
import software.amazon.awssdk.services.s3.model.ServerSideEncryption;
import software.amazon.awssdk.services.s3.model.StorageClass;
import software.amazon.awssdk.services.s3.model.UploadPartRequest;

/**
* Unit tests for {@link ObjectMetadata}.
Expand Down Expand Up @@ -78,4 +79,17 @@ void mapsEnumsToString() {
assertThat(result.checksumAlgorithm()).isEqualTo(ChecksumAlgorithm.CRC32);
}

@Test
void doesNotApplyContentLengthForPartUpload() {
long objectContentLength = 16L;
long partContentLength = 8L;
ObjectMetadata metadata = ObjectMetadata.builder().contentLength(objectContentLength).build();

UploadPartRequest.Builder builder = UploadPartRequest.builder().contentLength(partContentLength);
metadata.apply(builder);
UploadPartRequest result = builder.build();

assertThat(result.contentLength()).isEqualTo(partContentLength);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,28 @@ void contentTypeCanBeResolvedForLargeFiles(S3OutputStreamProvider s3OutputStream
assertThat(result.contentType()).isEqualTo("text/plain");
}

@TestAvailableOutputStreamProviders
void contentLengthCanBeSetForLargeFiles(S3OutputStreamProvider s3OutputStreamProvider) throws IOException {
int i = new Random().nextInt();
S3Resource resource = s3Resource("s3://first-bucket/new-file" + i + ".txt", s3OutputStreamProvider);
int fileSize = DEFAULT_PART_SIZE * 2;
resource.setObjectMetadata(ObjectMetadata.builder().contentLength((long) fileSize).build());

// create file larger than single part size in multipart upload to make sure that file can be successfully
// uploaded in parts
File file = File.createTempFile("s3resource", "test");
byte[] b = new byte[fileSize];
new Random().nextBytes(b);
Files.write(b, file);

try (OutputStream outputStream = resource.getOutputStream()) {
outputStream.write(Files.toByteArray(file));
}
GetObjectResponse result = client
.getObject(request -> request.bucket("first-bucket").key("new-file" + i + ".txt").build()).response();
assertThat(result.contentType()).isEqualTo("text/plain");
}

@TestAvailableOutputStreamProviders
void contentTypeCanBeResolvedForSmallFiles(S3OutputStreamProvider s3OutputStreamProvider) throws IOException {
S3Resource resource = s3Resource("s3://first-bucket/new-file.txt", s3OutputStreamProvider);
Expand Down

0 comments on commit 27eaa94

Please sign in to comment.