diff --git a/src/main/java/io/floci/aws/services/s3/S3Service.java b/src/main/java/io/floci/aws/services/s3/S3Service.java index b50652ce3b..3259b1bef2 100644 --- a/src/main/java/io/floci/aws/services/s3/S3Service.java +++ b/src/main/java/io/floci/aws/services/s3/S3Service.java @@ -8778,19 +8778,21 @@ public class S3Service implements Resettable, ResourceProvider { metadata.putAll(effectiveOptions.getReplacementMetadata()); } - String effectiveContentType = replaceMetadata && effectiveOptions.getContentType() != null - ? effectiveOptions.getContentType() + // REPLACE takes these headers from the request only; an omitted one is not carried over, + // and an omitted Content-Type becomes S3's binary/octet-stream. + String effectiveContentType = replaceMetadata + ? (effectiveOptions.getContentType() != null ? effectiveOptions.getContentType() : "binary/octet-stream") : source.getContentType(); String effectiveStorageClass = effectiveOptions.getStorageClass() != null ? effectiveOptions.getStorageClass() : source.getStorageClass(); - String effectiveContentEncoding = replaceMetadata && effectiveOptions.getContentEncoding() != null + String effectiveContentEncoding = replaceMetadata ? effectiveOptions.getContentEncoding() : source.getContentEncoding(); - String effectiveContentDisposition = replaceMetadata && effectiveOptions.getContentDisposition() != null + String effectiveContentDisposition = replaceMetadata ? effectiveOptions.getContentDisposition() : source.getContentDisposition(); - String effectiveCacheControl = replaceMetadata && effectiveOptions.getCacheControl() != null + String effectiveCacheControl = replaceMetadata ? effectiveOptions.getCacheControl() : source.getCacheControl(); String effectiveSseKmsKeyId = new ObjectEncryption(normalizedServerSideEncryption, diff --git a/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java b/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java index f5c0d89c4f..67c992f0a3 100644 --- a/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3IntegrationTest.java @@ -1867,6 +1867,83 @@ class S3IntegrationTest { given().delete("/cache-control-bucket"); } + // --- System headers under the copy metadata directive --- + + @Test + void copyObjectReplaceDropsOmittedSystemHeaders() { + putObjectWithSystemHeaders("replace-headers-bucket"); + + given() + .header("x-amz-copy-source", "/replace-headers-bucket/source.html") + .header("x-amz-metadata-directive", "REPLACE") + .header("x-amz-meta-owner", "team-b") + .when() + .put("/replace-headers-bucket/replaced.html") + .then() + .statusCode(200) + .body(containsString("CopyObjectResult")); + + given() + .when() + .head("/replace-headers-bucket/replaced.html") + .then() + .statusCode(200) + .header("x-amz-meta-owner", equalTo("team-b")) + .header("Content-Type", equalTo("binary/octet-stream")) + .header("Content-Encoding", nullValue()) + .header("Content-Disposition", nullValue()) + .header("Cache-Control", nullValue()); + + deleteBucketWithKeys("replace-headers-bucket", "source.html", "replaced.html"); + } + + @Test + void copyObjectCopyDirectiveKeepsSystemHeaders() { + putObjectWithSystemHeaders("copy-headers-bucket"); + + given() + .header("x-amz-copy-source", "/copy-headers-bucket/source.html") + .header("x-amz-metadata-directive", "COPY") + .when() + .put("/copy-headers-bucket/copied.html") + .then() + .statusCode(200) + .body(containsString("CopyObjectResult")); + + given() + .when() + .head("/copy-headers-bucket/copied.html") + .then() + .statusCode(200) + .header("Content-Type", startsWith("text/html")) + .header("Content-Encoding", equalTo("gzip")) + .header("Content-Disposition", equalTo("attachment; filename=\"page.html\"")) + .header("Cache-Control", equalTo("max-age=60")); + + deleteBucketWithKeys("copy-headers-bucket", "source.html", "copied.html"); + } + + private void putObjectWithSystemHeaders(String bucket) { + given().put("/" + bucket).then().statusCode(200); + given() + .contentType("text/html") + .header("Content-Encoding", "gzip") + .header("Content-Disposition", "attachment; filename=\"page.html\"") + .header("Cache-Control", "max-age=60") + .body("
page
") + .when() + .put("/" + bucket + "/source.html") + .then() + .statusCode(200); + } + + private void deleteBucketWithKeys(String bucket, String... keys) { + for (String key : keys) { + given().delete("/" + bucket + "/" + key); + } + given().delete("/" + bucket); + } + // --- Content-Disposition header preservation --- @Test