mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(s3): read one part of an object with GetObject and HeadObject partNumber (#5569)
GetObject and HeadObject ignored partNumber, so a client reading a multipart object one part at a time downloaded the whole object for every part, and never learned how many parts there were. The API Reference describes partNumber as "effectively" a ranged GET or HEAD for that part, with x-amz-mp-parts-count "only returned if you specify partNumber in your request and the object was uploaded as a multipart upload". Both now answer partNumber as S3 does in LocalStack's AWS-validated test_get_object_part, test_get_object_part_checksum and test_complete_multipart_parts_checksum captures: 206 with the part's Content-Range and Content-Length, the object's ETag, and x-amz-mp-parts-count for an object completed from a multipart upload. An object stored in one request is its own part 1. A part the object does not have is 416 InvalidPartNumber, naming PartNumberRequested and ActualPartCount, and partNumber with a Range header is 400 InvalidRequest. A part streams from storage like a range, so the parts of a large object are read one at a time without holding it. With checksum mode on, a part of an object with a COMPOSITE checksum gets that part's own checksum, of type COMPOSITE, and a part that is all of its object gets the object's checksum; any other part of a FULL_OBJECT multipart object gets none, as the captures show. A part number below 1 is 400 InvalidArgument, and one above the part count, past 10,000 or not, is the 416, as the AWS SDK for Java documents for partNumber; the 400 carries the wording and fields the captures record for UploadPart. Part 1 of an empty object succeeds, as the AWS SDKs' integration tests that download an empty object by part expect, with no Content-Range, which the CRT client reads only from a part with bytes, and so with 200, since a 206 must carry one (RFC 9110 15.3.7). GetObject's ranged response and HeadObject now build their common headers with GetObject's, given the length served.
This commit is contained in:
1 parent
c907eec9d2
commit
a88ef42b4b
6 files changed
+725
-29
No files matched your search
@@ -6,10 +6,14 @@ import software.amazon.awssdk.core.ResponseInputStream;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.BucketLocationConstraint;
|
||||
import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload;
|
||||
import software.amazon.awssdk.services.s3.model.CompletedPart;
|
||||
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest;
|
||||
import software.amazon.awssdk.services.s3.model.Delete;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteBucketTaggingRequest;
|
||||
@@ -40,10 +44,12 @@ import software.amazon.awssdk.services.s3.model.PutObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.s3.model.Tagging;
|
||||
import software.amazon.awssdk.services.s3.model.UploadPartRequest;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
@@ -551,6 +557,59 @@ class S3Test {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(34)
|
||||
void getObjectPartRoundTripsThroughSdk() throws Exception {
|
||||
String bucket = TestFixtures.uniqueName("part-sdk-bucket");
|
||||
String key = "parts.bin";
|
||||
byte[] firstPart = new byte[5 * 1024 * 1024];
|
||||
Arrays.fill(firstPart, (byte) 'a');
|
||||
String secondPart = "test content 123";
|
||||
s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
|
||||
// Closed in reverse order: the object, then its bucket.
|
||||
try (AutoCloseable deleteBucket = () ->
|
||||
s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());
|
||||
AutoCloseable deleteObject = () ->
|
||||
s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build())) {
|
||||
String uploadId = s3.createMultipartUpload(CreateMultipartUploadRequest.builder()
|
||||
.bucket(bucket).key(key).build()).uploadId();
|
||||
String firstETag = s3.uploadPart(UploadPartRequest.builder()
|
||||
.bucket(bucket).key(key).uploadId(uploadId).partNumber(1).build(),
|
||||
RequestBody.fromBytes(firstPart)).eTag();
|
||||
String secondETag = s3.uploadPart(UploadPartRequest.builder()
|
||||
.bucket(bucket).key(key).uploadId(uploadId).partNumber(2).build(),
|
||||
RequestBody.fromString(secondPart)).eTag();
|
||||
s3.completeMultipartUpload(CompleteMultipartUploadRequest.builder()
|
||||
.bucket(bucket).key(key).uploadId(uploadId)
|
||||
.multipartUpload(CompletedMultipartUpload.builder().parts(
|
||||
CompletedPart.builder().partNumber(1).eTag(firstETag).build(),
|
||||
CompletedPart.builder().partNumber(2).eTag(secondETag).build()).build())
|
||||
.build());
|
||||
long size = firstPart.length + secondPart.length();
|
||||
|
||||
ResponseBytes<GetObjectResponse> part = s3.getObjectAsBytes(GetObjectRequest.builder()
|
||||
.bucket(bucket).key(key).partNumber(2).build());
|
||||
|
||||
assertThat(part.asUtf8String()).isEqualTo(secondPart);
|
||||
assertThat(part.response().partsCount()).isEqualTo(2);
|
||||
assertThat(part.response().contentLength()).isEqualTo(16);
|
||||
assertThat(part.response().contentRange()).isEqualTo("bytes 5242880-5242895/" + size);
|
||||
|
||||
HeadObjectResponse head = s3.headObject(HeadObjectRequest.builder()
|
||||
.bucket(bucket).key(key).partNumber(1).build());
|
||||
|
||||
assertThat(head.partsCount()).isEqualTo(2);
|
||||
assertThat(head.contentLength()).isEqualTo(firstPart.length);
|
||||
|
||||
assertThatThrownBy(() -> s3.getObjectAsBytes(GetObjectRequest.builder()
|
||||
.bucket(bucket).key(key).partNumber(3).build()))
|
||||
.isInstanceOfSatisfying(S3Exception.class, e -> {
|
||||
assertThat(e.statusCode()).isEqualTo(416);
|
||||
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("InvalidPartNumber");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void assertInlineTaggingHeaderRejected(String rawTaggingHeader, int expectedStatus, String expectedErrorCode) {
|
||||
String bucket = TestFixtures.uniqueName("inline-tag-reject");
|
||||
String key = "rejected.txt";
|
||||
|
||||
@@ -108,6 +108,8 @@ Suffix ranges against empty objects return an empty `200` response, matching AWS
|
||||
|
||||
`HeadObject` honors `Range` as the API Reference describes: a satisfiable range changes `Content-Length` to the length of the range, and one that cannot be satisfied returns `416`, while a `Range` that is not one valid range, such as several ranges or a last byte before the first, is ignored. Like a ranged `GetObject`, it leaves out the whole-object checksum headers unless the range covers the whole of an object that is one part.
|
||||
|
||||
`GetObject` and `HeadObject` also read one part of an object with the `partNumber` query parameter, which works like a range covering that part: `206`, its `Content-Range` and `Content-Length`, the object's `ETag`, and, for an object completed from a multipart upload, `x-amz-mp-parts-count` with the number of its parts. An object stored in one request is its own part 1, and part 1 of an empty object returns an empty `200` with no `Content-Range`, which no byte range describes. A part streams from storage like a range, so the parts of a large object can be downloaded one at a time. With checksum mode on, a part of an object with a `COMPOSITE` checksum gets the part's own checksum, of type `COMPOSITE`, and a part that is all of its object gets the object's checksum; any other part of a `FULL_OBJECT` multipart object gets none. A part the object does not have, including any part number above 10,000, returns `416` `InvalidPartNumber` naming `PartNumberRequested` and `ActualPartCount`, `partNumber` together with a `Range` header returns `400` `InvalidRequest`, and a part number below 1 returns `400` `InvalidArgument`.
|
||||
|
||||
An `If-Range` header makes the range conditional, so a resumed download never mixes two versions of an object. The range is served while the validator still describes the object: a quoted ETag equal to the object's (a multipart ETag compared whole, `-N` suffix included), or an HTTP-date equal to its `Last-Modified`, an exact match as RFC 9110 section 13.1.5 requires, so a later date does not qualify. Otherwise, including a weak (`W/`) or malformed validator, `GetObject` returns the whole object with `200`. A presigned URL that signs `Range` must also sign an `If-Range` the request carries, or the request fails with `403 AccessDenied` naming it in `HeadersNotSigned`.
|
||||
|
||||
## S3 Select
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.s3.model.Part;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Object;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* The bytes of one part of an object, which GetObject and HeadObject serve for a {@code partNumber}
|
||||
* as they would a range covering that part. {@code part} is the part's record when the object was
|
||||
* completed from a multipart upload, and null for an object stored in one request, which is its own
|
||||
* only part.
|
||||
*/
|
||||
record PartRange(long first, long length, Part part) {
|
||||
|
||||
/** The message of S3's {@code InvalidPartNumber} error, for a part the object does not have. */
|
||||
static final String NOT_SATISFIABLE_MESSAGE = "The requested partnumber is not satisfiable";
|
||||
|
||||
/** The message of S3's {@code InvalidRequest} error for a request that names a range and a part. */
|
||||
static final String WITH_RANGE_MESSAGE = "Cannot specify both Range header and partNumber query parameter";
|
||||
|
||||
/**
|
||||
* The part a {@code partNumber} query parameter names, failing with the InvalidArgument S3 gives a part
|
||||
* number below 1. One above the object's part count, past 10,000 or past what a long holds, names a part
|
||||
* the object does not have, which is S3's 416 rather than a 400.
|
||||
*/
|
||||
static BigInteger parsePartNumber(String value) {
|
||||
try {
|
||||
BigInteger partNumber = new BigInteger(value.trim());
|
||||
if (partNumber.signum() > 0) {
|
||||
return partNumber;
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// What is not a number is no part number either, so it is rejected the same way.
|
||||
}
|
||||
throw new AwsException("InvalidArgument", "Part number must be an integer between 1 and 10000, inclusive",
|
||||
400, Map.of("ArgumentName", "partNumber", "ArgumentValue", value));
|
||||
}
|
||||
|
||||
/** Part {@code partNumber} of {@code obj}, or empty when the object has no such part. */
|
||||
static Optional<PartRange> of(S3Object obj, BigInteger partNumber) {
|
||||
if (!isMultipart(obj)) {
|
||||
return partNumber.equals(BigInteger.ONE)
|
||||
? Optional.of(new PartRange(0, obj.getSize(), null))
|
||||
: Optional.empty();
|
||||
}
|
||||
// The parts are kept in the order they were assembled in, so each starts where the last ended.
|
||||
long first = 0;
|
||||
for (Part part : obj.getParts()) {
|
||||
if (partNumber.equals(BigInteger.valueOf(part.getPartNumber()))) {
|
||||
return Optional.of(new PartRange(first, part.getSize(), part));
|
||||
}
|
||||
first += part.getSize();
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/** How many parts {@code obj} has; an object stored in one request has one. */
|
||||
static int partCount(S3Object obj) {
|
||||
return isMultipart(obj) ? obj.getParts().size() : 1;
|
||||
}
|
||||
|
||||
private static boolean isMultipart(S3Object obj) {
|
||||
List<Part> parts = obj.getParts();
|
||||
return parts != null && !parts.isEmpty();
|
||||
}
|
||||
|
||||
/** Whether this part holds all of an object of {@code size} bytes. */
|
||||
boolean coversObject(long size) {
|
||||
return first == 0 && length == size;
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code Content-Range} of this part of an object of {@code size} bytes, or null for a part of no
|
||||
* bytes, which no byte range describes.
|
||||
*/
|
||||
String contentRange(long size) {
|
||||
return length == 0 ? null : new ByteRange(first, first + length - 1).contentRange(size);
|
||||
}
|
||||
|
||||
/** This part's bytes, read from a stream over the whole object as they are needed. */
|
||||
InputStream slice(InputStream whole) {
|
||||
return new ByteRangeInputStream(whole, first, length);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@ import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
@@ -80,6 +81,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
@@ -1051,6 +1053,13 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
boolean includeChecksum = "ENABLED".equalsIgnoreCase(resolveHeaderOrQueryParam(checksumMode, uriInfo, "x-amz-checksum-mode"));
|
||||
String partNumber = uriInfo.getQueryParameters().getFirst("partNumber");
|
||||
if (partNumber != null) {
|
||||
Response partResponse = handlePartRequest("GetObject", bucket, key, obj, read.body(),
|
||||
partNumber, rangeHeader, overrides, includeChecksum);
|
||||
bodyHandedOff = partResponse.getEntity() instanceof StreamingOutput;
|
||||
return partResponse;
|
||||
}
|
||||
if (ByteRange.inBytes(rangeHeader) && ByteRange.rangeApplies(
|
||||
httpHeaders.getHeaderString("If-Range"), obj.getETag(), obj.getLastModified())) {
|
||||
Response rangeResponse = handleRangeRequest(
|
||||
@@ -1108,8 +1117,15 @@ public class S3Controller {
|
||||
private Response.ResponseBuilder objectResponseHeaders(Response.ResponseBuilder resp, S3Object obj,
|
||||
ResponseHeaderOverrides overrides,
|
||||
boolean includeChecksum) {
|
||||
return objectResponseHeaders(resp, obj, obj.getSize(), overrides, includeChecksum);
|
||||
}
|
||||
|
||||
/** The same headers for a response carrying {@code contentLength} bytes of the object. */
|
||||
private Response.ResponseBuilder objectResponseHeaders(Response.ResponseBuilder resp, S3Object obj,
|
||||
long contentLength, ResponseHeaderOverrides overrides,
|
||||
boolean includeChecksum) {
|
||||
resp.header("Content-Type", overrides.contentType() != null ? overrides.contentType() : obj.getContentType())
|
||||
.header("Content-Length", obj.getSize())
|
||||
.header("Content-Length", contentLength)
|
||||
.header("ETag", obj.getETag())
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()))
|
||||
.header("Accept-Ranges", "bytes");
|
||||
@@ -1142,19 +1158,49 @@ public class S3Controller {
|
||||
in.transferTo(output);
|
||||
}
|
||||
};
|
||||
var resp = Response.status(206)
|
||||
Response.ResponseBuilder resp = Response.status(206)
|
||||
.entity(stream)
|
||||
.header("Content-Type", overrides.contentType() != null ? overrides.contentType() : obj.getContentType())
|
||||
.header("Content-Length", range.length())
|
||||
.header("Content-Range", range.contentRange(totalSize))
|
||||
.header("ETag", obj.getETag())
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()))
|
||||
.header("Accept-Ranges", "bytes");
|
||||
if (obj.getVersionId() != null) {
|
||||
resp.header("x-amz-version-id", obj.getVersionId());
|
||||
.header("Content-Range", range.contentRange(totalSize));
|
||||
return objectResponseHeaders(resp, obj, range.length(), overrides,
|
||||
includeChecksum && rangeCarriesObjectChecksum(range, obj)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serves part {@code partNumber} of {@code obj}, as GetObject and HeadObject do for a
|
||||
* {@code partNumber}: like a range covering that part, with the count of the object's parts when it
|
||||
* was uploaded in parts. HeadObject passes no {@code body}. A part of no bytes, which no range
|
||||
* describes, is answered as the whole of an empty object would be.
|
||||
*/
|
||||
private Response handlePartRequest(String operation, String bucket, String key, S3Object obj, InputStream body,
|
||||
String partNumber, String rangeHeader, ResponseHeaderOverrides overrides,
|
||||
boolean includeChecksum) {
|
||||
if (rangeHeader != null) {
|
||||
throw new AwsException("InvalidRequest", PartRange.WITH_RANGE_MESSAGE, 400);
|
||||
}
|
||||
appendObjectHeaders(resp, obj, overrides, includeChecksum && rangeCarriesObjectChecksum(range, obj));
|
||||
return resp.build();
|
||||
BigInteger requested = PartRange.parsePartNumber(partNumber);
|
||||
Optional<PartRange> found = PartRange.of(obj, requested);
|
||||
if (found.isEmpty()) {
|
||||
emitCloudTrailEvent(operation, bucket, key, 0L, 0L, "InvalidPartNumber", PartRange.NOT_SATISFIABLE_MESSAGE);
|
||||
return invalidPartNumberResponse(requested, PartRange.partCount(obj));
|
||||
}
|
||||
PartRange part = found.get();
|
||||
emitCloudTrailEvent(operation, bucket, key, 0L, part.length(), null, null);
|
||||
String contentRange = part.contentRange(obj.getSize());
|
||||
Function<StreamingOutput, Response> build = stream -> {
|
||||
Response.ResponseBuilder resp = Response.status(contentRange != null ? 206 : 200).entity(stream);
|
||||
if (contentRange != null) {
|
||||
resp.header("Content-Range", contentRange);
|
||||
}
|
||||
if (part.part() != null) {
|
||||
resp.header("x-amz-mp-parts-count", PartRange.partCount(obj));
|
||||
}
|
||||
objectResponseHeaders(resp, obj, part.length(), overrides, false);
|
||||
if (includeChecksum) {
|
||||
appendChecksumHeaders(resp, partChecksum(obj, part));
|
||||
}
|
||||
return resp.build();
|
||||
};
|
||||
return body == null ? build.apply(null) : streamingResponse(part.slice(body), build);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1165,12 +1211,41 @@ public class S3Controller {
|
||||
* ranges get none.
|
||||
*/
|
||||
private static boolean rangeCarriesObjectChecksum(ByteRange range, S3Object obj) {
|
||||
return range.first() == 0 && range.last() == obj.getSize() - 1 && checksumIsOnlyPartsChecksum(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the stored checksum is that of the object's only part: an object stored in one request, or
|
||||
* completed from one part with a full-object checksum. A composite checksum is never one part's own.
|
||||
*/
|
||||
private static boolean checksumIsOnlyPartsChecksum(S3Object obj) {
|
||||
S3Checksum checksum = obj.getChecksum();
|
||||
return range.first() == 0 && range.last() == obj.getSize() - 1
|
||||
&& (obj.getParts() == null || obj.getParts().size() <= 1)
|
||||
return (obj.getParts() == null || obj.getParts().size() <= 1)
|
||||
&& checksum != null && checksum.getChecksumType() != ChecksumType.COMPOSITE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The checksum S3 returns with a part: of an object with a composite checksum, the part's own, which
|
||||
* the composite is built from, and otherwise the object's when the part is all of the object, as for
|
||||
* a range covering it.
|
||||
*/
|
||||
private static S3Checksum partChecksum(S3Object obj, PartRange part) {
|
||||
S3Checksum checksum = obj.getChecksum();
|
||||
if (checksum != null && checksum.getChecksumType() == ChecksumType.COMPOSITE && part.part() != null) {
|
||||
ChecksumAlgorithm algorithm = checksum.algorithm();
|
||||
S3Checksum stored = part.part().getChecksum();
|
||||
String value = algorithm == null || stored == null ? null : stored.valueFor(algorithm);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
S3Checksum partChecksum = new S3Checksum();
|
||||
partChecksum.setValueFor(algorithm, value);
|
||||
partChecksum.setChecksumType(ChecksumType.COMPOSITE);
|
||||
return partChecksum;
|
||||
}
|
||||
return part.coversObject(obj.getSize()) && checksumIsOnlyPartsChecksum(obj) ? checksum : null;
|
||||
}
|
||||
|
||||
private Response websiteObjectResponse(S3Service.ObjectRead read, ResponseHeaderOverrides overrides) {
|
||||
return fullObjectResponse(read.object(), read.body(), overrides, false);
|
||||
}
|
||||
@@ -1191,7 +1266,7 @@ public class S3Controller {
|
||||
.elem("Message", ByteRange.NOT_SATISFIABLE_MESSAGE)
|
||||
.elem("RangeRequested", rangeHeader)
|
||||
.elem("ActualObjectSize", totalSize)
|
||||
.elem("RequestId", java.util.UUID.randomUUID().toString())
|
||||
.elem("RequestId", UUID.randomUUID().toString())
|
||||
.end("Error")
|
||||
.build();
|
||||
return Response.status(416)
|
||||
@@ -1201,6 +1276,21 @@ public class S3Controller {
|
||||
.build();
|
||||
}
|
||||
|
||||
/** S3's 416 for a part the object does not have, naming the part asked for and how many it has. */
|
||||
private static Response invalidPartNumberResponse(BigInteger partNumber, int actualPartCount) {
|
||||
String xml = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("Error")
|
||||
.elem("Code", "InvalidPartNumber")
|
||||
.elem("Message", PartRange.NOT_SATISFIABLE_MESSAGE)
|
||||
.elem("PartNumberRequested", partNumber.toString())
|
||||
.elem("ActualPartCount", actualPartCount)
|
||||
.elem("RequestId", UUID.randomUUID().toString())
|
||||
.end("Error")
|
||||
.build();
|
||||
return Response.status(416).entity(xml).type(MediaType.APPLICATION_XML).build();
|
||||
}
|
||||
|
||||
@HEAD
|
||||
@Path("/{bucket}/{key:.+}")
|
||||
public Response headObject(@PathParam("bucket") String bucket,
|
||||
@@ -1216,7 +1306,7 @@ public class S3Controller {
|
||||
@HeaderParam("If-None-Match") String ifNoneMatch,
|
||||
@HeaderParam("If-Modified-Since") String ifModifiedSince,
|
||||
@HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince,
|
||||
@HeaderParam("x-amz-checksum-mode") String checksumMode,
|
||||
@HeaderParam("x-amz-checksum-mode") String checksumModeHeader,
|
||||
@Context UriInfo uriInfo,
|
||||
@Context HttpHeaders httpHeaders) {
|
||||
S3Service.RequestAuthorization authorization = S3Service.RequestAuthorization.unsigned();
|
||||
@@ -1262,6 +1352,13 @@ public class S3Controller {
|
||||
return xmlErrorResponse(new AwsException("InvalidRequest",
|
||||
"Request specific response headers cannot be used for anonymous GET requests.", 400));
|
||||
}
|
||||
boolean checksumMode = "ENABLED".equalsIgnoreCase(
|
||||
resolveHeaderOrQueryParam(checksumModeHeader, uriInfo, "x-amz-checksum-mode"));
|
||||
String partNumber = uriInfo.getQueryParameters().getFirst("partNumber");
|
||||
if (partNumber != null) {
|
||||
return headOnlyResponse(handlePartRequest("HeadObject", bucket, key, obj, null, partNumber,
|
||||
httpHeaders.getHeaderString("Range"), overrides, checksumMode));
|
||||
}
|
||||
// "If the Range is satisfiable, only the ContentLength is affected in the response. If the
|
||||
// Range is not satisfiable, S3 returns a 416." A Range that is not one valid range is ignored.
|
||||
long contentLength = obj.getSize();
|
||||
@@ -1279,19 +1376,9 @@ public class S3Controller {
|
||||
return headOnlyResponse(invalidRangeResponse(rangeHeader, obj.getSize()));
|
||||
}
|
||||
}
|
||||
var resp = Response.ok()
|
||||
.header("Content-Type", overrides.contentType() != null ? overrides.contentType() : obj.getContentType())
|
||||
.header("Content-Length", contentLength)
|
||||
.header("ETag", obj.getETag())
|
||||
.header("Last-Modified", RFC_822.format(obj.getLastModified()))
|
||||
.header("Accept-Ranges", "bytes");
|
||||
if (obj.getVersionId() != null) {
|
||||
resp.header("x-amz-version-id", obj.getVersionId());
|
||||
}
|
||||
// As on a ranged GetObject, the stored checksum is sent only for a range it describes.
|
||||
boolean includeChecksum = checksumDescribesRange
|
||||
&& "ENABLED".equalsIgnoreCase(resolveHeaderOrQueryParam(checksumMode, uriInfo, "x-amz-checksum-mode"));
|
||||
appendObjectHeaders(resp, obj, overrides, includeChecksum);
|
||||
Response.ResponseBuilder resp = objectResponseHeaders(Response.ok(), obj, contentLength, overrides,
|
||||
checksumDescribesRange && checksumMode);
|
||||
emitCloudTrailEvent("HeadObject", bucket, key, 0L, obj.getSize(), null, null);
|
||||
return resp.build();
|
||||
} catch (AwsException e) {
|
||||
@@ -3472,7 +3559,7 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
String xml = xmlBuilder
|
||||
.elem("RequestId", java.util.UUID.randomUUID().toString())
|
||||
.elem("RequestId", UUID.randomUUID().toString())
|
||||
.end("Error")
|
||||
.build();
|
||||
return Response.status(e.getHttpStatus()).entity(xml).type(MediaType.APPLICATION_XML).build();
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.s3.model.Part;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Object;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PartRangeTest {
|
||||
|
||||
@Test
|
||||
void aPartNumberOfOneOrMoreIsRead() {
|
||||
assertEquals(BigInteger.ONE, PartRange.parsePartNumber("1"));
|
||||
assertEquals(BigInteger.valueOf(10000), PartRange.parsePartNumber("10000"));
|
||||
// Past 10,000, or past what a long holds, is left to the part lookup, which finds no such part.
|
||||
assertEquals(BigInteger.valueOf(10001), PartRange.parsePartNumber("10001"));
|
||||
assertEquals(new BigInteger("9223372036854775808"), PartRange.parsePartNumber("9223372036854775808"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPartNumberBelowOneOrNotANumberIsInvalidArgumentNamingIt() {
|
||||
for (String value : List.of("0", "-1", "abc", "", "1.5")) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> PartRange.parsePartNumber(value));
|
||||
|
||||
assertEquals("InvalidArgument", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("Part number must be an integer between 1 and 10000, inclusive", error.getMessage());
|
||||
assertEquals(Map.of("ArgumentName", "partNumber", "ArgumentValue", value), error.getExtendedData());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachPartStartsWhereTheOneBeforeItEnded() {
|
||||
// Part numbers need not be consecutive; a part is found by its own number.
|
||||
S3Object obj = multipartObject(new Part(1, "\"a\"", 5), new Part(2, "\"b\"", 3), new Part(4, "\"c\"", 2));
|
||||
|
||||
PartRange second = PartRange.of(obj, BigInteger.TWO).orElseThrow();
|
||||
assertEquals(5, second.first());
|
||||
assertEquals(3, second.length());
|
||||
assertSame(obj.getParts().get(1), second.part());
|
||||
assertEquals("bytes 5-7/10", second.contentRange(10));
|
||||
|
||||
PartRange last = PartRange.of(obj, BigInteger.valueOf(4)).orElseThrow();
|
||||
assertEquals(8, last.first());
|
||||
assertEquals(2, last.length());
|
||||
assertEquals("bytes 8-9/10", last.contentRange(10));
|
||||
|
||||
assertTrue(PartRange.of(obj, BigInteger.valueOf(3)).isEmpty());
|
||||
assertTrue(PartRange.of(obj, BigInteger.valueOf(5)).isEmpty());
|
||||
assertTrue(PartRange.of(obj, BigInteger.valueOf(10001)).isEmpty());
|
||||
assertTrue(PartRange.of(obj, new BigInteger("9223372036854775808")).isEmpty());
|
||||
assertEquals(3, PartRange.partCount(obj));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anObjectStoredInOneRequestIsItsOwnOnlyPart() {
|
||||
S3Object obj = new S3Object("bucket", "key", 8, "text/plain", "\"etag\"");
|
||||
|
||||
PartRange only = PartRange.of(obj, BigInteger.ONE).orElseThrow();
|
||||
assertEquals(0, only.first());
|
||||
assertEquals(8, only.length());
|
||||
assertNull(only.part());
|
||||
assertTrue(only.coversObject(8));
|
||||
assertEquals("bytes 0-7/8", only.contentRange(8));
|
||||
|
||||
assertTrue(PartRange.of(obj, BigInteger.TWO).isEmpty());
|
||||
assertEquals(1, PartRange.partCount(obj));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPartOfNoBytesHasNoContentRange() {
|
||||
S3Object obj = new S3Object("bucket", "key", 0, "text/plain", "\"etag\"");
|
||||
PartRange empty = PartRange.of(obj, BigInteger.ONE).orElseThrow();
|
||||
|
||||
assertNull(empty.contentRange(0));
|
||||
assertTrue(empty.coversObject(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyAPartHoldingAllOfTheObjectCoversIt() {
|
||||
S3Object obj = multipartObject(new Part(1, "\"a\"", 5), new Part(2, "\"b\"", 3));
|
||||
|
||||
assertFalse(PartRange.of(obj, BigInteger.ONE).orElseThrow().coversObject(8));
|
||||
assertFalse(PartRange.of(obj, BigInteger.TWO).orElseThrow().coversObject(8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSliceIsThePartsBytes() throws IOException {
|
||||
S3Object obj = multipartObject(new Part(1, "\"a\"", 5), new Part(2, "\"b\"", 3));
|
||||
InputStream whole = new ByteArrayInputStream("helloabc".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
try (InputStream part = PartRange.of(obj, BigInteger.TWO).orElseThrow().slice(whole)) {
|
||||
assertEquals("abc", new String(part.readAllBytes(), StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private static S3Object multipartObject(Part... parts) {
|
||||
long size = 0;
|
||||
for (Part part : parts) {
|
||||
size += part.getSize();
|
||||
}
|
||||
S3Object obj = new S3Object("bucket", "key", size, "application/octet-stream", "\"etag-" + parts.length + "\"");
|
||||
obj.setParts(List.of(parts));
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
+338
@@ -0,0 +1,338 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.emptyString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
/**
|
||||
* GetObject and HeadObject with {@code partNumber}, following what S3 answers in LocalStack's
|
||||
* AWS-validated {@code test_get_object_part}, {@code test_get_object_part_checksum} and
|
||||
* {@code test_complete_multipart_parts_checksum_*} captures: an object completed from a 5 MiB part and
|
||||
* the 16 bytes of "test content 123".
|
||||
*/
|
||||
@QuarkusTest
|
||||
class S3GetObjectPartIntegrationTest {
|
||||
|
||||
private static final byte[] FIRST_PART = filled(5 * 1024 * 1024);
|
||||
private static final String SECOND_PART = "test content 123";
|
||||
private static final long OBJECT_SIZE = FIRST_PART.length + SECOND_PART.length();
|
||||
|
||||
@Test
|
||||
void getObject_partOfAMultipartObject_isServedLikeItsRange() {
|
||||
String bucket = createBucket("part-get");
|
||||
String eTag = uploadInTwoParts(bucket, "object.bin", null, null);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("Content-Length", equalTo("16"))
|
||||
.header("Content-Range", equalTo("bytes 5242880-5242895/" + OBJECT_SIZE))
|
||||
.header("x-amz-mp-parts-count", equalTo("2"))
|
||||
.header("ETag", equalTo(eTag))
|
||||
.body(equalTo(SECOND_PART));
|
||||
|
||||
byte[] first = given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=1")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("Content-Range", equalTo("bytes 0-5242879/" + OBJECT_SIZE))
|
||||
.header("x-amz-mp-parts-count", equalTo("2"))
|
||||
.extract().asByteArray();
|
||||
assertArrayEquals(FIRST_PART, first);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headObject_partOfAMultipartObject_describesThatPart() {
|
||||
String bucket = createBucket("part-head");
|
||||
String eTag = uploadInTwoParts(bucket, "object.bin", null, null);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.head("/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("Content-Length", equalTo("16"))
|
||||
.header("Content-Range", equalTo("bytes 5242880-5242895/" + OBJECT_SIZE))
|
||||
.header("x-amz-mp-parts-count", equalTo("2"))
|
||||
.header("ETag", equalTo(eTag));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partTheObjectDoesNotHave_isInvalidPartNumber() {
|
||||
String bucket = createBucket("part-missing");
|
||||
uploadInTwoParts(bucket, "object.bin", null, null);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=3")
|
||||
.then()
|
||||
.statusCode(416)
|
||||
.body(containsString("<Code>InvalidPartNumber</Code>"
|
||||
+ "<Message>The requested partnumber is not satisfiable</Message>"
|
||||
+ "<PartNumberRequested>3</PartNumberRequested><ActualPartCount>2</ActualPartCount>"));
|
||||
given()
|
||||
.when()
|
||||
.head("/" + bucket + "/object.bin?partNumber=3")
|
||||
.then()
|
||||
.statusCode(416)
|
||||
.body(emptyString());
|
||||
// Past 10,000 is still a part the object does not have, not an invalid part number.
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=10001")
|
||||
.then()
|
||||
.statusCode(416)
|
||||
.body(containsString("<Code>InvalidPartNumber</Code>"))
|
||||
.body(containsString("<PartNumberRequested>10001</PartNumberRequested><ActualPartCount>2</ActualPartCount>"));
|
||||
// So is one past what a long holds, named as it was asked for.
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=9223372036854775808")
|
||||
.then()
|
||||
.statusCode(416)
|
||||
.body(containsString("<Code>InvalidPartNumber</Code>"))
|
||||
.body(containsString("<PartNumberRequested>9223372036854775808</PartNumberRequested>"
|
||||
+ "<ActualPartCount>2</ActualPartCount>"));
|
||||
given()
|
||||
.when()
|
||||
.head("/" + bucket + "/object.bin?partNumber=9223372036854775808")
|
||||
.then()
|
||||
.statusCode(416)
|
||||
.body(emptyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partNumberWithARange_isInvalidRequest() {
|
||||
String bucket = createBucket("part-range");
|
||||
uploadInTwoParts(bucket, "object.bin", null, null);
|
||||
|
||||
for (String method : new String[] {"GET", "HEAD"}) {
|
||||
given()
|
||||
.header("Range", "bytes=0-8")
|
||||
.when()
|
||||
.request(method, "/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
given()
|
||||
.header("Range", "bytes=0-8")
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.body(containsString("<Code>InvalidRequest</Code>"
|
||||
+ "<Message>Cannot specify both Range header and partNumber query parameter</Message>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partNumberBelowOne_isInvalidArgument() {
|
||||
String bucket = createBucket("part-invalid");
|
||||
given().body("test-123").when().put("/" + bucket + "/object.txt").then().statusCode(200);
|
||||
|
||||
for (String partNumber : new String[] {"0", "-1", "abc"}) {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.txt?partNumber=" + partNumber)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidArgument</Code>"
|
||||
+ "<Message>Part number must be an integer between 1 and 10000, inclusive</Message>"))
|
||||
.body(containsString("<ArgumentName>partNumber</ArgumentName>"
|
||||
+ "<ArgumentValue>" + partNumber + "</ArgumentValue>"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partOfAnObjectStoredInOneRequest_isTheWholeObjectAsPartOne() {
|
||||
String bucket = createBucket("part-single");
|
||||
String crc32 = S3Checksum.crc32Base64("test-123".getBytes(StandardCharsets.UTF_8));
|
||||
given()
|
||||
.header("x-amz-checksum-crc32", crc32)
|
||||
.body("test-123")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.get("/" + bucket + "/object.txt?partNumber=1")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("Content-Range", equalTo("bytes 0-7/8"))
|
||||
.header("x-amz-mp-parts-count", nullValue())
|
||||
.header("x-amz-checksum-crc32", equalTo(crc32))
|
||||
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
|
||||
.body(equalTo("test-123"));
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.txt?partNumber=2")
|
||||
.then()
|
||||
.statusCode(416)
|
||||
.body(containsString("<PartNumberRequested>2</PartNumberRequested><ActualPartCount>1</ActualPartCount>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partOfAnEmptyObject_isAnsweredAsTheWholeEmptyObject() {
|
||||
String bucket = createBucket("part-empty");
|
||||
given().body(new byte[0]).when().put("/" + bucket + "/empty.txt").then().statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/empty.txt?partNumber=1")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Content-Length", equalTo("0"))
|
||||
.header("Content-Range", nullValue())
|
||||
.body(emptyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partOfACompositeChecksumObject_carriesThatPartsChecksum() {
|
||||
String bucket = createBucket("part-composite");
|
||||
uploadInTwoParts(bucket, "object.bin", "CRC32C", null);
|
||||
|
||||
for (String method : new String[] {"GET", "HEAD"}) {
|
||||
given()
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.request(method, "/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("x-amz-checksum-crc32c",
|
||||
equalTo(S3Checksum.crc32cBase64(SECOND_PART.getBytes(StandardCharsets.UTF_8))))
|
||||
.header("x-amz-checksum-type", equalTo("COMPOSITE"));
|
||||
}
|
||||
given()
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=1")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("x-amz-checksum-crc32c", equalTo(S3Checksum.crc32cBase64(FIRST_PART)));
|
||||
// Without checksum mode, no checksum is sent.
|
||||
given()
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("x-amz-checksum-crc32c", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_partOfAFullObjectChecksumObjectOfSeveralParts_carriesNoChecksum() {
|
||||
String bucket = createBucket("part-full-object");
|
||||
uploadInTwoParts(bucket, "object.bin", "CRC32C", "FULL_OBJECT");
|
||||
|
||||
given()
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=2")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("x-amz-checksum-crc32c", nullValue())
|
||||
.header("x-amz-checksum-type", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_onlyPartOfAFullObjectChecksumObject_carriesTheObjectsChecksum() {
|
||||
String bucket = createBucket("part-one-full-object");
|
||||
String uploadId = createUpload(bucket, "object.bin", "CRC32C", "FULL_OBJECT");
|
||||
String partETag = uploadPart(bucket, "object.bin", uploadId, 1, SECOND_PART.getBytes(StandardCharsets.UTF_8));
|
||||
complete(bucket, "object.bin", uploadId, partXml(1, partETag, null));
|
||||
|
||||
given()
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.get("/" + bucket + "/object.bin?partNumber=1")
|
||||
.then()
|
||||
.statusCode(206)
|
||||
.header("x-amz-mp-parts-count", equalTo("1"))
|
||||
.header("x-amz-checksum-crc32c",
|
||||
equalTo(S3Checksum.crc32cBase64(SECOND_PART.getBytes(StandardCharsets.UTF_8))))
|
||||
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"));
|
||||
}
|
||||
|
||||
/** Uploads {@link #FIRST_PART} and {@link #SECOND_PART} as parts 1 and 2, returning the object's ETag. */
|
||||
private static String uploadInTwoParts(String bucket, String key, String checksumAlgorithm, String checksumType) {
|
||||
String uploadId = createUpload(bucket, key, checksumAlgorithm, checksumType);
|
||||
byte[] second = SECOND_PART.getBytes(StandardCharsets.UTF_8);
|
||||
String firstETag = uploadPart(bucket, key, uploadId, 1, FIRST_PART);
|
||||
String secondETag = uploadPart(bucket, key, uploadId, 2, second);
|
||||
// A composite checksum is completed from the checksum of every part.
|
||||
boolean composite = checksumAlgorithm != null && checksumType == null;
|
||||
return complete(bucket, key, uploadId,
|
||||
partXml(1, firstETag, composite ? S3Checksum.crc32cBase64(FIRST_PART) : null)
|
||||
+ partXml(2, secondETag, composite ? S3Checksum.crc32cBase64(second) : null));
|
||||
}
|
||||
|
||||
private static String createUpload(String bucket, String key, String checksumAlgorithm, String checksumType) {
|
||||
RequestSpecification request = given();
|
||||
if (checksumAlgorithm != null) {
|
||||
request.header("x-amz-checksum-algorithm", checksumAlgorithm);
|
||||
}
|
||||
if (checksumType != null) {
|
||||
request.header("x-amz-checksum-type", checksumType);
|
||||
}
|
||||
return request
|
||||
.when()
|
||||
.post("/" + bucket + "/" + key + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
}
|
||||
|
||||
private static String uploadPart(String bucket, String key, String uploadId, int partNumber, byte[] data) {
|
||||
return given()
|
||||
.body(data)
|
||||
.when()
|
||||
.put("/" + bucket + "/" + key + "?partNumber=" + partNumber + "&uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().header("ETag");
|
||||
}
|
||||
|
||||
private static String complete(String bucket, String key, String uploadId, String partsXml) {
|
||||
return given()
|
||||
.contentType("application/xml")
|
||||
.body("<CompleteMultipartUpload>" + partsXml + "</CompleteMultipartUpload>")
|
||||
.when()
|
||||
.post("/" + bucket + "/" + key + "?uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ETag>"))
|
||||
.extract().xmlPath().getString("CompleteMultipartUploadResult.ETag");
|
||||
}
|
||||
|
||||
private static String partXml(int partNumber, String eTag, String crc32c) {
|
||||
return "<Part><PartNumber>" + partNumber + "</PartNumber><ETag>" + eTag + "</ETag>"
|
||||
+ (crc32c != null ? "<ChecksumCRC32C>" + crc32c + "</ChecksumCRC32C>" : "") + "</Part>";
|
||||
}
|
||||
|
||||
private static String createBucket(String prefix) {
|
||||
String bucket = prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static byte[] filled(int size) {
|
||||
byte[] data = new byte[size];
|
||||
Arrays.fill(data, (byte) 'a');
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user