mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat: implement UploadPartCopy for S3 multipart uploads (#98)
This commit is contained in:
1 parent
eef66983d9
commit
d1b9a9ca6d
3 files changed
+110
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in new issue
Block a user