Light, fluffy, and always free - The AWS Local Emulator alternative
feat: implement UploadPartCopy for S3 multipart uploads (#98)
Jakubd1b9a9ca6d8d
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 7ed3020145..3d4d67fae4 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -327,6 +327,9 @@ public class S3Controller {}if (uploadId != null && partNumber != null) {+ if (copySource != null && !copySource.isEmpty()) {+ return handleUploadPartCopy(copySource, bucket, key, uploadId, partNumber, httpHeaders);+ }byte[] partData = decodeAwsChunked(body, contentEncoding, contentSha256);validateChecksumHeaders(httpHeaders, partData);String eTag = s3Service.uploadPart(bucket, key, uploadId, partNumber, partData);@@ -1102,6 +1105,28 @@ public class S3Controller {return Response.ok(xml).build();}+ private Response handleUploadPartCopy(String copySource, String destBucket, String destKey,+ String uploadId, int partNumber, HttpHeaders httpHeaders) {+ String source = copySource.startsWith("/") ? copySource.substring(1) : copySource;+ int slashIndex = source.indexOf('/');+ if (slashIndex < 0) {+ throw new AwsException("InvalidArgument", "Invalid copy source: " + copySource, 400);+ }+ String sourceBucket = source.substring(0, slashIndex);+ String sourceKey = source.substring(slashIndex + 1);+ String copySourceRange = httpHeaders.getHeaderString("x-amz-copy-source-range");+ String eTag = s3Service.uploadPartCopy(destBucket, destKey, uploadId, partNumber,+ sourceBucket, sourceKey, copySourceRange);+ String xml = new XmlBuilder()+ .raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")+ .start("CopyPartResult", AwsNamespaces.S3)+ .elem("LastModified", ISO_FORMAT.format(java.time.Instant.now()))+ .elem("ETag", eTag)+ .end("CopyPartResult")+ .build();+ return Response.ok(xml).type(MediaType.APPLICATION_XML).build();+ }+private Response handleGetObjectAttributes(String bucket, String key, String versionId,String objectAttributesHeader, Integer maxParts,Integer partNumberMarker) {diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.javaindex b94fb6070d..fa4381fd73 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java@@ -814,6 +814,26 @@ public class S3Service {return eTag;}+ public String uploadPartCopy(String destBucket, String destKey, String uploadId, int partNumber,+ String sourceBucket, String sourceKey, String copySourceRange) {+ S3Object source = getObject(sourceBucket, sourceKey);+ byte[] data = source.getData();++ if (copySourceRange != null && !copySourceRange.isBlank()) {+ // format: "bytes=START-END" (inclusive on both ends)+ String range = copySourceRange.startsWith("bytes=") ? copySourceRange.substring(6) : copySourceRange;+ int dash = range.indexOf('-');+ if (dash < 0) {+ throw new AwsException("InvalidArgument", "Invalid x-amz-copy-source-range: " + copySourceRange, 400);+ }+ int start = Integer.parseInt(range.substring(0, dash).trim());+ int end = Integer.parseInt(range.substring(dash + 1).trim());+ data = Arrays.copyOfRange(data, start, end + 1);+ }++ return uploadPart(destBucket, destKey, uploadId, partNumber, data);+ }+public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers) {MultipartUpload upload = multipartUploads.get(uploadId);if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.javaindex eb678d8d09..4c376a4059 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java@@ -178,8 +178,73 @@ class S3MultipartIntegrationTest {@Test@Order(11)+ void uploadPartCopy() {+ // Put a source object+ given()+ .body("ABCDEFGHIJ")+ .when()+ .put("/" + BUCKET + "/source-for-copy.bin")+ .then()+ .statusCode(200);++ // Initiate multipart upload for destination+ String copyUploadId = given()+ .when()+ .post("/" + BUCKET + "/copy-dest.bin?uploads")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");++ // UploadPartCopy full source+ given()+ .header("x-amz-copy-source", "/" + BUCKET + "/source-for-copy.bin")+ .when()+ .put("/" + BUCKET + "/copy-dest.bin?uploadId=" + copyUploadId + "&partNumber=1")+ .then()+ .statusCode(200)+ .body(containsString("<CopyPartResult"))+ .body(containsString("<ETag>"));++ // UploadPartCopy with range (bytes 2-5 → "CDEF")+ given()+ .header("x-amz-copy-source", "/" + BUCKET + "/source-for-copy.bin")+ .header("x-amz-copy-source-range", "bytes=2-5")+ .when()+ .put("/" + BUCKET + "/copy-dest.bin?uploadId=" + copyUploadId + "&partNumber=2")+ .then()+ .statusCode(200)+ .body(containsString("<CopyPartResult"))+ .body(containsString("<ETag>"));++ // Complete the upload+ String completeXml = """+ <CompleteMultipartUpload>+ <Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>+ <Part><PartNumber>2</PartNumber><ETag>etag2</ETag></Part>+ </CompleteMultipartUpload>""";+ given()+ .contentType("application/xml")+ .body(completeXml)+ .when()+ .post("/" + BUCKET + "/copy-dest.bin?uploadId=" + copyUploadId)+ .then()+ .statusCode(200);++ // Verify contents: full source + ranged slice+ given()+ .when()+ .get("/" + BUCKET + "/copy-dest.bin")+ .then()+ .statusCode(200)+ .body(equalTo("ABCDEFGHIJCDEF"));+ }++ @Test+ @Order(12)void cleanUp() {given().when().delete("/" + BUCKET + "/" + KEY).then().statusCode(204);+ given().when().delete("/" + BUCKET + "/source-for-copy.bin").then().statusCode(204);+ given().when().delete("/" + BUCKET + "/copy-dest.bin").then().statusCode(204);given().when().delete("/" + BUCKET).then().statusCode(204);}}