Search in sources :

Example 1 with COPY_SOURCE_IF_UNMODIFIED_SINCE

use of org.apache.hadoop.ozone.s3.util.S3Consts.COPY_SOURCE_IF_UNMODIFIED_SINCE in project ozone by apache.

the class ObjectEndpoint method createMultipartKey.

private Response createMultipartKey(String bucket, String key, long length, int partNumber, String uploadID, InputStream body) throws IOException, OS3Exception {
    try {
        OzoneBucket ozoneBucket = getBucket(bucket);
        String copyHeader;
        OzoneOutputStream ozoneOutputStream = null;
        if ("STREAMING-AWS4-HMAC-SHA256-PAYLOAD".equals(headers.getHeaderString("x-amz-content-sha256"))) {
            body = new SignedChunksInputStream(body);
        }
        try {
            ozoneOutputStream = ozoneBucket.createMultipartKey(key, length, partNumber, uploadID);
            copyHeader = headers.getHeaderString(COPY_SOURCE_HEADER);
            if (copyHeader != null) {
                Pair<String, String> result = parseSourceHeader(copyHeader);
                String sourceBucket = result.getLeft();
                String sourceKey = result.getRight();
                Long sourceKeyModificationTime = getBucket(sourceBucket).getKey(sourceKey).getModificationTime().toEpochMilli();
                String copySourceIfModifiedSince = headers.getHeaderString(COPY_SOURCE_IF_MODIFIED_SINCE);
                String copySourceIfUnmodifiedSince = headers.getHeaderString(COPY_SOURCE_IF_UNMODIFIED_SINCE);
                if (!checkCopySourceModificationTime(sourceKeyModificationTime, copySourceIfModifiedSince, copySourceIfUnmodifiedSince)) {
                    throw newError(PRECOND_FAILED, sourceBucket + "/" + sourceKey);
                }
                try (OzoneInputStream sourceObject = getBucket(sourceBucket).readKey(sourceKey)) {
                    String range = headers.getHeaderString(COPY_SOURCE_HEADER_RANGE);
                    if (range != null) {
                        RangeHeader rangeHeader = RangeHeaderParserUtil.parseRangeHeader(range, 0);
                        final long skipped = sourceObject.skip(rangeHeader.getStartOffset());
                        if (skipped != rangeHeader.getStartOffset()) {
                            throw new EOFException("Bytes to skip: " + rangeHeader.getStartOffset() + " actual: " + skipped);
                        }
                        IOUtils.copyLarge(sourceObject, ozoneOutputStream, 0, rangeHeader.getEndOffset() - rangeHeader.getStartOffset() + 1);
                    } else {
                        IOUtils.copy(sourceObject, ozoneOutputStream);
                    }
                }
            } else {
                IOUtils.copy(body, ozoneOutputStream);
            }
        } finally {
            if (ozoneOutputStream != null) {
                ozoneOutputStream.close();
            }
        }
        assert ozoneOutputStream != null;
        OmMultipartCommitUploadPartInfo omMultipartCommitUploadPartInfo = ozoneOutputStream.getCommitUploadPartInfo();
        String eTag = omMultipartCommitUploadPartInfo.getPartName();
        if (copyHeader != null) {
            return Response.ok(new CopyPartResult(eTag)).build();
        } else {
            return Response.ok().header("ETag", eTag).build();
        }
    } catch (OMException ex) {
        if (ex.getResult() == ResultCodes.NO_SUCH_MULTIPART_UPLOAD_ERROR) {
            throw newError(NO_SUCH_UPLOAD, uploadID, ex);
        } else if (ex.getResult() == ResultCodes.PERMISSION_DENIED) {
            throw newError(S3ErrorTable.ACCESS_DENIED, bucket + "/" + key, ex);
        }
        throw ex;
    }
}
Also used : OzoneInputStream(org.apache.hadoop.ozone.client.io.OzoneInputStream) SignedChunksInputStream(org.apache.hadoop.ozone.s3.SignedChunksInputStream) OzoneOutputStream(org.apache.hadoop.ozone.client.io.OzoneOutputStream) RangeHeader(org.apache.hadoop.ozone.s3.util.RangeHeader) OzoneBucket(org.apache.hadoop.ozone.client.OzoneBucket) OmMultipartCommitUploadPartInfo(org.apache.hadoop.ozone.om.helpers.OmMultipartCommitUploadPartInfo) OptionalLong(java.util.OptionalLong) EOFException(java.io.EOFException) OMException(org.apache.hadoop.ozone.om.exceptions.OMException)

Example 2 with COPY_SOURCE_IF_UNMODIFIED_SINCE

use of org.apache.hadoop.ozone.s3.util.S3Consts.COPY_SOURCE_IF_UNMODIFIED_SINCE in project ozone by apache.

the class TestMultipartUploadWithCopy method uploadPartWithCopy.

private Part uploadPartWithCopy(String key, String uploadID, int partNumber, String keyOrigin, String range, String ifModifiedSinceStr, String ifUnmodifiedSinceStr) throws IOException, OS3Exception {
    Map<String, String> additionalHeaders = new HashMap<>();
    additionalHeaders.put(COPY_SOURCE_HEADER, keyOrigin);
    if (range != null) {
        additionalHeaders.put(COPY_SOURCE_HEADER_RANGE, range);
    }
    if (ifModifiedSinceStr != null) {
        additionalHeaders.put(COPY_SOURCE_IF_MODIFIED_SINCE, ifModifiedSinceStr);
    }
    if (ifUnmodifiedSinceStr != null) {
        additionalHeaders.put(COPY_SOURCE_IF_UNMODIFIED_SINCE, ifUnmodifiedSinceStr);
    }
    setHeaders(additionalHeaders);
    ByteArrayInputStream body = new ByteArrayInputStream("".getBytes(UTF_8));
    Response response = REST.put(OzoneConsts.S3_BUCKET, key, 0, partNumber, uploadID, body);
    assertEquals(200, response.getStatus());
    CopyPartResult result = (CopyPartResult) response.getEntity();
    assertNotNull(result.getETag());
    assertNotNull(result.getLastModified());
    Part part = new Part();
    part.seteTag(result.getETag());
    part.setPartNumber(partNumber);
    return part;
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(org.apache.hadoop.ozone.s3.endpoint.CompleteMultipartUploadRequest.Part)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 EOFException (java.io.EOFException)1 HashMap (java.util.HashMap)1 OptionalLong (java.util.OptionalLong)1 Response (javax.ws.rs.core.Response)1 OzoneBucket (org.apache.hadoop.ozone.client.OzoneBucket)1 OzoneInputStream (org.apache.hadoop.ozone.client.io.OzoneInputStream)1 OzoneOutputStream (org.apache.hadoop.ozone.client.io.OzoneOutputStream)1 OMException (org.apache.hadoop.ozone.om.exceptions.OMException)1 OmMultipartCommitUploadPartInfo (org.apache.hadoop.ozone.om.helpers.OmMultipartCommitUploadPartInfo)1 SignedChunksInputStream (org.apache.hadoop.ozone.s3.SignedChunksInputStream)1 Part (org.apache.hadoop.ozone.s3.endpoint.CompleteMultipartUploadRequest.Part)1 RangeHeader (org.apache.hadoop.ozone.s3.util.RangeHeader)1