diff --git a/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/ObjectMetadata.java b/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/ObjectMetadata.java index 8913910ac..95a3a51ea 100644 --- a/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/ObjectMetadata.java +++ b/spring-cloud-aws-s3/src/main/java/io/awspring/cloud/s3/ObjectMetadata.java @@ -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); } diff --git a/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/ObjectMetadataTests.java b/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/ObjectMetadataTests.java index d8ed4ea80..ee5688c46 100644 --- a/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/ObjectMetadataTests.java +++ b/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/ObjectMetadataTests.java @@ -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}. @@ -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); + } + } diff --git a/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java b/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java index fce24e04f..425082333 100644 --- a/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java +++ b/spring-cloud-aws-s3/src/test/java/io/awspring/cloud/s3/S3ResourceIntegrationTests.java @@ -219,6 +219,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);