/** * <p> * Populates the specified request with the numerous options available in * <code>CopyObjectRequest</code>. * </p> * * @param request * The request to populate with headers to represent all the * options expressed in the <code>CopyPartRequest</code> object. * @param copyPartRequest * The object containing all the options for copying an object in * Amazon S3. */ private static void populateRequestWithCopyPartParameters(Request<?> request, CopyPartRequest copyPartRequest) { String copySourceHeader = "/" + SdkHttpUtils.urlEncode(copyPartRequest.getSourceBucketName(), true) + "/" + SdkHttpUtils.urlEncode(copyPartRequest.getSourceKey(), true); if (copyPartRequest.getSourceVersionId() != null) { copySourceHeader += "?versionId=" + copyPartRequest.getSourceVersionId(); } request.addHeader("x-amz-copy-source", copySourceHeader); addDateHeader(request, Headers.COPY_SOURCE_IF_MODIFIED_SINCE, copyPartRequest.getModifiedSinceConstraint()); addDateHeader(request, Headers.COPY_SOURCE_IF_UNMODIFIED_SINCE, copyPartRequest.getUnmodifiedSinceConstraint()); addStringListHeader(request, Headers.COPY_SOURCE_IF_MATCH, copyPartRequest.getMatchingETagConstraints()); addStringListHeader(request, Headers.COPY_SOURCE_IF_NO_MATCH, copyPartRequest.getNonmatchingETagConstraints()); if ( copyPartRequest.getFirstByte() != null && copyPartRequest.getLastByte() != null ) { String range = "bytes=" + copyPartRequest.getFirstByte() + "-" + copyPartRequest.getLastByte(); request.addHeader(Headers.COPY_PART_RANGE, range); } // Populate the SSE-C parameters for the destination object populateSourceSSE_C(request, copyPartRequest.getSourceSSECustomerKey()); populateSSE_C(request, copyPartRequest.getDestinationSSECustomerKey()); }
/** * Submits a callable for each part to be copied to our thread pool and * records its corresponding Future. */ private void copyPartsInParallel(CopyPartRequestFactory requestFactory) { while (requestFactory.hasMoreRequests()) { if (threadPool.isShutdown()) throw new CancellationException( "TransferManager has been shutdown"); CopyPartRequest request = requestFactory.getNextCopyPartRequest(); futures.add(threadPool.submit(new CopyPartCallable(s3, request))); } }
/** * Constructs a copy part requests and returns it. * * @return Returns a new copy part request */ public synchronized CopyPartRequest getNextCopyPartRequest() { final long partSize = Math.min(optimalPartSize, remainingBytes); CopyPartRequest req = new CopyPartRequest() .withSourceBucketName(origReq.getSourceBucketName()) .withSourceKey(origReq.getSourceKey()) .withUploadId(uploadId) .withPartNumber(partNumber++) .withDestinationBucketName(origReq.getDestinationBucketName()) .withDestinationKey(origReq.getDestinationKey()) .withSourceVersionId(origReq.getSourceVersionId()) .withFirstByte(Long.valueOf(offset)) .withLastByte(Long.valueOf(offset + partSize - 1)) .withSourceSSECustomerKey(origReq.getSourceSSECustomerKey()) .withDestinationSSECustomerKey(origReq.getDestinationSSECustomerKey()) .withRequesterPays(origReq.isRequesterPays()) // other meta data .withMatchingETagConstraints(origReq.getMatchingETagConstraints()) .withModifiedSinceConstraint(origReq.getModifiedSinceConstraint()) .withNonmatchingETagConstraints(origReq.getNonmatchingETagConstraints()) .withSourceVersionId(origReq.getSourceVersionId()) .withUnmodifiedSinceConstraint(origReq.getUnmodifiedSinceConstraint()) // general meta data .withGeneralProgressListener(origReq.getGeneralProgressListener()) .withRequestMetricCollector(origReq.getRequestMetricCollector()) ; offset += partSize; remainingBytes -= partSize; return req; }
@Override public final CopyPartResult copyPartSecurely(CopyPartRequest copyPartRequest) { String uploadId = copyPartRequest.getUploadId(); T uploadContext = multipartUploadContexts.get(uploadId); CopyPartResult result = s3.copyPart(copyPartRequest); if (uploadContext != null && !uploadContext.hasFinalPartBeenSeen()) uploadContext.setHasFinalPartBeenSeen(true); return result; }
@Override public CopyPartResult copyPartSecurely(CopyPartRequest req) { return defaultCryptoMode == EncryptionOnly ? eo.copyPartSecurely(req) : ae.copyPartSecurely(req) ; }
@Override public CopyPartResult copyPart(CopyPartRequest copyPartRequest) throws AmazonClientException, AmazonServiceException { return delegate.copyPart(copyPartRequest); }
public CopyPartCallable(AmazonS3 s3, CopyPartRequest request) { this.s3 = s3; this.request = request; }
@Override public CopyPartResult copyPart(CopyPartRequest copyPartRequest) { return crypto.copyPartSecurely(copyPartRequest); }
@Override public CopyPartResult copyPart(CopyPartRequest req) { return AmazonS3EncryptionClient.super.copyPart(req); }
@Override public CopyPartResult copyPart(CopyPartRequest copyPartRequest) { return call(() -> getDelegate().copyPart(copyPartRequest)); }
private CompleteMultipartUploadResult copyMultipartFile(Bucket srcBucket, Bucket targetBucket, String fileName, long size) { // Create lists to hold copy responses List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); // Step 2: Initialize InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(targetBucket.getName(), targetBucket.getPath() + SEPARATOR + fileName); InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initiateRequest); // Step 4. Copy parts. long partSize = 5 * (long)Math.pow(2.0, 20.0); // 5 MB long bytePosition = 0; for (int i = 1; bytePosition < size; i++) { // Step 5. Save copy response. CopyPartRequest copyRequest = new CopyPartRequest() .withDestinationBucketName(targetBucket.getName()) .withDestinationKey(targetBucket.getPath() + SEPARATOR + fileName) .withSourceBucketName(srcBucket.getName()) .withSourceKey(srcBucket.getPath() + SEPARATOR + fileName) .withUploadId(initResult.getUploadId()) .withFirstByte(bytePosition) .withLastByte(bytePosition + partSize -1 >= size ? size - 1 : bytePosition + partSize - 1) .withPartNumber(i); copyResponses.add(s3Client.copyPart(copyRequest)); bytePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( targetBucket.getName(), targetBucket.getPath() + SEPARATOR + fileName, initResult.getUploadId(), GetETags(copyResponses)); // Step 7. Complete copy operation. CompleteMultipartUploadResult completeUploadResponse = s3Client.completeMultipartUpload(completeRequest); return completeUploadResponse; }
@Override public CopyPartResult copyPart(CopyPartRequest copyPartRequest) throws AmazonClientException { return null; }
@Test public void testMultipartCopy() throws Exception { // B2 requires two parts to issue an MPU assumeTrue(!blobStoreType.equals("b2")); String sourceBlobName = "testMultipartCopy-source"; String targetBlobName = "testMultipartCopy-target"; ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); client.putObject(containerName, sourceBlobName, BYTE_SOURCE.openStream(), metadata); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest(containerName, targetBlobName); InitiateMultipartUploadResult initResult = client.initiateMultipartUpload(initiateRequest); String uploadId = initResult.getUploadId(); CopyPartRequest copyRequest = new CopyPartRequest() .withDestinationBucketName(containerName) .withDestinationKey(targetBlobName) .withSourceBucketName(containerName) .withSourceKey(sourceBlobName) .withUploadId(uploadId) .withFirstByte(0L) .withLastByte(BYTE_SOURCE.size() - 1) .withPartNumber(1); CopyPartResult copyPartResult = client.copyPart(copyRequest); CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( containerName, targetBlobName, uploadId, ImmutableList.of(copyPartResult.getPartETag())); CompleteMultipartUploadResult completeUploadResponse = client.completeMultipartUpload(completeRequest); S3Object object = client.getObject(containerName, targetBlobName); assertThat(object.getObjectMetadata().getContentLength()).isEqualTo( BYTE_SOURCE.size()); try (InputStream actual = object.getObjectContent(); InputStream expected = BYTE_SOURCE.openStream()) { assertThat(actual).hasContentEqualTo(expected); } }
@Override public CopyPartResult copyPart(CopyPartRequest copyPartRequest) throws AmazonClientException, AmazonServiceException { // TODO Auto-generated method stub return null; }
public abstract CopyPartResult copyPart(CopyPartRequest req);
public abstract CopyPartResult copyPartSecurely(CopyPartRequest req);
public CopyPartResult copyPart(CopyPartRequest req);