mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(s3): stream UploadPartCopy ranges into the part (#5162)
UploadPartCopy read the copied range into a byte array and stored it through the byte-array UploadPart path, so one copied part was capped near 2 GiB with EntityTooLarge, while S3 allows a part of up to 5 GiB. The range is now read from the source stream as it is needed, by a new CopyRangeInputStream that skips to the range on its first read and ends after its last byte, and written straight into the part through the streaming UploadPart path, so neither the source nor the part has to fit in memory in the disk-backed modes. A range over S3's 5 GiB part limit is rejected with EntityTooLarge, as the S3 API Reference error table words it, before anything is copied. Memory mode keeps its limit of about 2 GiB.
This commit is contained in:
1 parent
9c0102559d
commit
8fee66ae61
6 files changed
+197
-35
No files matched your search
@@ -0,0 +1,59 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.S3Service.CopySourceRange;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* The bytes of one copy-source range, read from a stream over the whole source as they are needed.
|
||||
* The first read skips to the range's first byte, so nothing of the source is touched until the
|
||||
* part is about to be written, and the stream ends after the range's last byte. A source that ends
|
||||
* before the range does fails the read.
|
||||
*/
|
||||
final class CopyRangeInputStream extends InputStream {
|
||||
|
||||
private final InputStream source;
|
||||
private final long first;
|
||||
private long remaining;
|
||||
private boolean positioned;
|
||||
|
||||
CopyRangeInputStream(InputStream source, CopySourceRange range) {
|
||||
this.source = source;
|
||||
this.first = range.first();
|
||||
this.remaining = range.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
byte[] single = new byte[1];
|
||||
int read = read(single, 0, 1);
|
||||
return read < 0 ? -1 : single[0] & 0xFF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
if (length == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!positioned) {
|
||||
source.skipNBytes(first);
|
||||
positioned = true;
|
||||
}
|
||||
if (remaining == 0) {
|
||||
return -1;
|
||||
}
|
||||
int read = source.read(buffer, offset, (int) Math.min(length, remaining));
|
||||
if (read < 0) {
|
||||
throw new EOFException("Copy source ended " + remaining + " bytes before the end of the requested range");
|
||||
}
|
||||
remaining -= read;
|
||||
return read;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
source.close();
|
||||
}
|
||||
}
|
||||
@@ -2256,6 +2256,9 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
// S3 copies at most 5 GiB in one CopyObject; a larger object is copied with UploadPartCopy.
|
||||
static final long MAX_COPY_OBJECT_SOURCE_SIZE = 5L * 1024 * 1024 * 1024;
|
||||
|
||||
// The most one part of a multipart upload can hold, per S3's multipart upload limits.
|
||||
static final long MAX_PART_SIZE = 5L * 1024 * 1024 * 1024;
|
||||
|
||||
static void requireCopyableSize(S3Object source) {
|
||||
if (source.getSize() > MAX_COPY_OBJECT_SOURCE_SIZE) {
|
||||
throw new AwsException("InvalidRequest",
|
||||
@@ -3978,9 +3981,9 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
SseCustomerHeaders copySourceSseCustomerHeaders,
|
||||
SseCustomerHeaders sseCustomerHeaders,
|
||||
CopySourceConditions copySourceConditions) {
|
||||
byte[] data;
|
||||
// The source is streamed the way GetObject serves it and only the copied range is read, so
|
||||
// the source can be any size.
|
||||
// The source is streamed the way GetObject serves it, and only the copied range is read,
|
||||
// straight into the part the way a streamed UploadPart body is, so neither the source nor
|
||||
// the part has to fit in memory in the disk-backed modes.
|
||||
try (ObjectRead read = openObject(sourceBucket, sourceKey, sourceVersionId)) {
|
||||
S3Object source = read.object();
|
||||
checkCopySourcePreconditions(source, copySourceConditions);
|
||||
@@ -3989,18 +3992,17 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
copySourceSseCustomerHeaders.key(),
|
||||
copySourceSseCustomerHeaders.keyMd5());
|
||||
CopySourceRange range = CopySourceRange.parse(copySourceRange, source.getSize());
|
||||
if (range.length() > MAX_IN_MEMORY_OBJECT_SIZE) {
|
||||
LOG.warnv("UploadPartCopy of {0} bytes into upload {1} is over the {2} bytes one part can hold in Floci; copy the source in smaller ranges",
|
||||
range.length(), uploadId, MAX_IN_MEMORY_OBJECT_SIZE);
|
||||
throw new AwsException("EntityTooLarge", "Your proposed upload exceeds the maximum allowed object size.", 400);
|
||||
if (range.length() > MAX_PART_SIZE) {
|
||||
throw new AwsException("EntityTooLarge",
|
||||
"Your proposed upload exceeds the maximum allowed object size.", 400);
|
||||
}
|
||||
data = readRange(read.body(), range);
|
||||
return storePart(destBucket, destKey, uploadId, partNumber,
|
||||
new CopyRangeInputStream(read.body(), range), UploadChecksums.NONE,
|
||||
sseCustomerHeaders.algorithm(), sseCustomerHeaders.key(), sseCustomerHeaders.keyMd5())
|
||||
.getETag();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Failed to read the copy source", e);
|
||||
throw new UncheckedIOException("Failed to close the copy source", e);
|
||||
}
|
||||
|
||||
return uploadPart(destBucket, destKey, uploadId, partNumber, data,
|
||||
sseCustomerHeaders.algorithm(), sseCustomerHeaders.key(), sseCustomerHeaders.keyMd5());
|
||||
}
|
||||
|
||||
/** An inclusive byte range of a copy source. Offsets are longs, since a source can pass 2 GiB. */
|
||||
@@ -4063,17 +4065,6 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads exactly the bytes of {@code range} from {@code body}, skipping the bytes before it. */
|
||||
static byte[] readRange(InputStream body, CopySourceRange range) throws IOException {
|
||||
body.skipNBytes(range.first());
|
||||
byte[] data = body.readNBytes((int) range.length());
|
||||
if (data.length != range.length()) {
|
||||
throw new IOException("Copy source ended " + (range.length() - data.length)
|
||||
+ " bytes before the end of the requested range");
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
return completeMultipartUpload(bucket, key, uploadId, partNumbers, Map.of(), Map.of(), checksumType,
|
||||
|
||||
@@ -17,6 +17,9 @@ import java.util.function.Function;
|
||||
*/
|
||||
record UploadChecksums(String contentMd5, Map<ChecksumAlgorithm, String> claimed) {
|
||||
|
||||
/** No integrity headers, as for the part an UploadPartCopy reads from another object. */
|
||||
static final UploadChecksums NONE = new UploadChecksums(null, Map.of());
|
||||
|
||||
// The order the checksums have always been checked in, so a body failing several reports the same one.
|
||||
private static final List<ChecksumAlgorithm> CHECK_ORDER = List.of(ChecksumAlgorithm.SHA1,
|
||||
ChecksumAlgorithm.SHA256, ChecksumAlgorithm.CRC32, ChecksumAlgorithm.CRC32C, ChecksumAlgorithm.CRC64NVME);
|
||||
|
||||
Reference in new issue
Block a user