fix(s3): parse copy-source versionId for CopyObject and part copy iss… (#751)

* fix(s3): parse copy-source versionId for CopyObject and part copy issue-748
* fix(s3): support versionId in CopyObject and UploadPartCopy sources - issue-748
* fix(s3): added new line at the end of class S3Controller - issue-748
This commit is contained in:
Asha Somayajula authored and GitHub committed 2026-05-09 08:25:50 -05:00
1 parent 2bed9ec3ad
commit ce2012de45
5 files changed
+364 -48

No files matched your search

@@ -1511,14 +1511,15 @@ public class S3Controller {
throw new AwsException("InvalidArgument", "Invalid copy source: " + copySource, 400);
}
String sourceBucket = decodedSource.substring(0, slashIndex);
String sourceKey = decodedSource.substring(slashIndex + 1);
String pathAfterBucket = decodedSource.substring(slashIndex + 1);
ParsedCopySource sourceObject = parseCopySourceObject(pathAfterBucket);
String copyContentEncoding = toPersistedContentEncoding(httpHeaders.getHeaderString("Content-Encoding"));
String copyContentDisposition = httpHeaders.getHeaderString("Content-Disposition");
String copyCacheControl = httpHeaders.getHeaderString("Cache-Control");
String copyServerSideEncryption = httpHeaders.getHeaderString("x-amz-server-side-encryption");
String cannedAcl = httpHeaders.getHeaderString("x-amz-acl");
S3Object copy = s3Service.copyObject(sourceBucket, sourceKey, destBucket, destKey,
S3Object copy = s3Service.copyObject(sourceBucket, sourceObject.objectKey(), destBucket, destKey,
sourceObject.versionId(),
new CopyObjectOptions()
.withMetadataDirective(httpHeaders.getHeaderString("x-amz-metadata-directive"))
.withReplacementMetadata(extractUserMetadata(httpHeaders))
@@ -1556,10 +1557,11 @@ public class S3Controller {
throw new AwsException("InvalidArgument", "Invalid copy source: " + copySource, 400);
}
String sourceBucket = decodedSource.substring(0, slashIndex);
String sourceKey = decodedSource.substring(slashIndex + 1);
String pathAfterBucket = decodedSource.substring(slashIndex + 1);
ParsedCopySource sourceObject = parseCopySourceObject(pathAfterBucket);
String copySourceRange = httpHeaders.getHeaderString("x-amz-copy-source-range");
String eTag = s3Service.uploadPartCopy(destBucket, destKey, uploadId, partNumber,
sourceBucket, sourceKey, copySourceRange);
sourceBucket, sourceObject.objectKey(), sourceObject.versionId(), copySourceRange);
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("CopyPartResult", AwsNamespaces.S3)
@@ -2209,4 +2211,46 @@ public class S3Controller {
s3Service.putBucketWebsite(bucket, new WebsiteConfiguration(indexDoc, errorDoc));
return Response.ok().build();
}
/**
* Splits the {@code CopyObject}/{@code UploadPartCopy} copy-source remainder into S3 object key and
* optional source {@code versionId}.
* <ul>
* <li><b>Input:</b> decoded {@code x-amz-copy-source} with bucket already removed (substring after
* the {@code '/'} that follows the bucket). Both {@code handleCopyObject} and
* {@code handleUploadPartCopy} compute this as {@code pathAfterBucket}.</li>
* <li><b>Key:</b> substring before the first {@code '?'} if any; keys may contain more {@code '/'}
* segments.</li>
* <li><b>{@code versionId}:</b> first {@code versionId} query pair, when present (raw value after
* {@code '='}). Other query pairs are ignored.</li>
* </ul>
*
* @param pathAfterBucket object key alone, or key with query (for example {@code dir/k.txt?versionId=uuid})
* @return key without trailing query plus {@code versionId} value, or {@code null} version when absent
*/
private ParsedCopySource parseCopySourceObject(String pathAfterBucket) {
int queryStart = pathAfterBucket.indexOf('?');
if (queryStart < 0) {
return new ParsedCopySource(pathAfterBucket, null);
}
String objectKey = pathAfterBucket.substring(0, queryStart);
String query = pathAfterBucket.substring(queryStart + 1);
String versionId = null;
for (String pair : query.split("&")) {
int eq = pair.indexOf('=');
if (eq <= 0) {
continue;
}
String name = pair.substring(0, eq);
String value = pair.substring(eq + 1);
if ("versionId".equals(name)) {
versionId = value;
break;
}
}
return new ParsedCopySource(objectKey, versionId);
}
private record ParsedCopySource(String objectKey, String versionId) {
}
}
@@ -636,6 +636,11 @@ public class S3Service {
return copyObject(sourceBucket, sourceKey, destBucket, destKey, new CopyObjectOptions());
}
public S3Object copyObject(String sourceBucket, String sourceKey,
String destBucket, String destKey, String versionId) {
return copyObject(sourceBucket, sourceKey, destBucket, destKey, null, new CopyObjectOptions());
}
public S3Object copyObject(String sourceBucket, String sourceKey,
String destBucket, String destKey,
String metadataDirective, Map<String, String> replacementMetadata,
@@ -666,50 +671,35 @@ public class S3Service {
.withAcl(acl));
}
public S3Object copyObject(String sourceBucket, String sourceKey,
String destBucket, String destKey, String versionId,
String metadataDirective, Map<String, String> replacementMetadata,
String storageClass, String contentType, String contentEncoding,
String contentDisposition, String cacheControl, String serverSideEncryption, String acl) {
return copyObject(sourceBucket, sourceKey, destBucket, destKey, versionId,
new CopyObjectOptions()
.withMetadataDirective(metadataDirective)
.withReplacementMetadata(replacementMetadata)
.withStorageClass(storageClass)
.withContentType(contentType)
.withContentEncoding(contentEncoding)
.withContentDisposition(contentDisposition)
.withCacheControl(cacheControl)
.withServerSideEncryption(serverSideEncryption)
.withAcl(acl));
}
public S3Object copyObject(String sourceBucket, String sourceKey,
String destBucket, String destKey, String versionId, CopyObjectOptions options)
{
S3Object source = getObject(sourceBucket, sourceKey, versionId);
return copyS3Object(sourceBucket, sourceKey,
destBucket, destKey, source, options);
}
public S3Object copyObject(String sourceBucket, String sourceKey,
String destBucket, String destKey, CopyObjectOptions options) {
S3Object source = getObject(sourceBucket, sourceKey);
ensureBucketExists(destBucket);
CopyObjectOptions effectiveOptions = options != null ? options : new CopyObjectOptions();
String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption());
boolean replaceMetadata = "REPLACE".equalsIgnoreCase(effectiveOptions.getMetadataDirective());
Map<String, String> metadata = replaceMetadata ? new LinkedHashMap<>() : new LinkedHashMap<>(source.getMetadata());
if (replaceMetadata && effectiveOptions.getReplacementMetadata() != null) {
metadata.putAll(effectiveOptions.getReplacementMetadata());
}
String effectiveContentType = replaceMetadata && effectiveOptions.getContentType() != null
? effectiveOptions.getContentType()
: source.getContentType();
String effectiveStorageClass = effectiveOptions.getStorageClass() != null
? effectiveOptions.getStorageClass()
: source.getStorageClass();
String effectiveContentEncoding = replaceMetadata && effectiveOptions.getContentEncoding() != null
? effectiveOptions.getContentEncoding()
: source.getContentEncoding();
String effectiveContentDisposition = replaceMetadata && effectiveOptions.getContentDisposition() != null
? effectiveOptions.getContentDisposition()
: source.getContentDisposition();
String effectiveCacheControl = replaceMetadata && effectiveOptions.getCacheControl() != null
? effectiveOptions.getCacheControl()
: source.getCacheControl();
String effectiveServerSideEncryption = normalizedServerSideEncryption != null
? normalizedServerSideEncryption
: source.getServerSideEncryption();
S3Object copy = storeObject(destBucket, destKey, source.getData(), effectiveContentType, metadata,
source.getChecksum(), source.getParts(),
new PutObjectOptions()
.withStorageClass(effectiveStorageClass)
.withContentEncoding(effectiveContentEncoding)
.withContentDisposition(effectiveContentDisposition)
.withCacheControl(effectiveCacheControl)
.withServerSideEncryption(effectiveServerSideEncryption)
.withAcl(effectiveOptions.getAcl()));
copy.setETag(source.getETag());
LOG.debugv("Copied object: {0}/{1} -> {2}/{3}", sourceBucket, sourceKey, destBucket, destKey);
fireNotifications(destBucket, destKey, "ObjectCreated:Copy", copy);
return copy;
return copyS3Object(sourceBucket, sourceKey, destBucket, destKey, source, options);
}
// --- Versioning Operations ---
@@ -1093,8 +1083,9 @@ public class S3Service {
}
public String uploadPartCopy(String destBucket, String destKey, String uploadId, int partNumber,
String sourceBucket, String sourceKey, String copySourceRange) {
S3Object source = getObject(sourceBucket, sourceKey);
String sourceBucket, String sourceKey, String sourceVersionId,
String copySourceRange) {
S3Object source = getObject(sourceBucket, sourceKey, sourceVersionId);
byte[] data = source.getData();
if (copySourceRange != null && !copySourceRange.isBlank()) {
@@ -2026,4 +2017,49 @@ public class S3Service {
LOG.errorv(e, "Failed to delete directory: {0}", dir);
}
}
private S3Object copyS3Object(String sourceBucket, String sourceKey,
String destBucket, String destKey, S3Object source, CopyObjectOptions options) {
ensureBucketExists(destBucket);
CopyObjectOptions effectiveOptions = options != null ? options : new CopyObjectOptions();
String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption());
boolean replaceMetadata = "REPLACE".equalsIgnoreCase(effectiveOptions.getMetadataDirective());
Map<String, String> metadata = replaceMetadata ? new LinkedHashMap<>() : new LinkedHashMap<>(source.getMetadata());
if (replaceMetadata && effectiveOptions.getReplacementMetadata() != null) {
metadata.putAll(effectiveOptions.getReplacementMetadata());
}
String effectiveContentType = replaceMetadata && effectiveOptions.getContentType() != null
? effectiveOptions.getContentType()
: source.getContentType();
String effectiveStorageClass = effectiveOptions.getStorageClass() != null
? effectiveOptions.getStorageClass()
: source.getStorageClass();
String effectiveContentEncoding = replaceMetadata && effectiveOptions.getContentEncoding() != null
? effectiveOptions.getContentEncoding()
: source.getContentEncoding();
String effectiveContentDisposition = replaceMetadata && effectiveOptions.getContentDisposition() != null
? effectiveOptions.getContentDisposition()
: source.getContentDisposition();
String effectiveCacheControl = replaceMetadata && effectiveOptions.getCacheControl() != null
? effectiveOptions.getCacheControl()
: source.getCacheControl();
String effectiveServerSideEncryption = normalizedServerSideEncryption != null
? normalizedServerSideEncryption
: source.getServerSideEncryption();
S3Object copy = storeObject(destBucket, destKey, source.getData(), effectiveContentType, metadata,
source.getChecksum(), source.getParts(),
new PutObjectOptions()
.withStorageClass(effectiveStorageClass)
.withContentEncoding(effectiveContentEncoding)
.withContentDisposition(effectiveContentDisposition)
.withCacheControl(effectiveCacheControl)
.withServerSideEncryption(effectiveServerSideEncryption)
.withAcl(effectiveOptions.getAcl()));
copy.setETag(source.getETag());
LOG.debugv("Copied object: {0}/{1} -> {2}/{3}", sourceBucket, sourceKey, destBucket, destKey);
fireNotifications(destBucket, destKey, "ObjectCreated:Copy", copy);
return copy;
}
}