AWS AmazonS3 documentation change
Summary
Replaced inline Java multipart copy code example with API reference link
Security assessment
Change involves documentation structure improvement by moving code samples to external resources. No security implications or security-related content added/modified.
Diff
diff --git a/AmazonS3/latest/userguide/CopyingObjectsMPUapi.md b/AmazonS3/latest/userguide/CopyingObjectsMPUapi.md index 74d4b87be..73811de40 100644 --- a//AmazonS3/latest/userguide/CopyingObjectsMPUapi.md +++ b//AmazonS3/latest/userguide/CopyingObjectsMPUapi.md @@ -54,95 +54 @@ Java -The following example shows how to use the Amazon S3 low-level Java API to perform a multipart copy. For instructions on creating and testing a working sample, see [Getting Started](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/getting-started.html) in the AWS SDK for Java Developer Guide. - - - import com.amazonaws.AmazonServiceException; - import com.amazonaws.SdkClientException; - import com.amazonaws.auth.profile.ProfileCredentialsProvider; - import com.amazonaws.regions.Regions; - import com.amazonaws.services.s3.AmazonS3; - import com.amazonaws.services.s3.AmazonS3ClientBuilder; - import com.amazonaws.services.s3.model.*; - - import java.io.IOException; - import java.util.ArrayList; - import java.util.List; - - public class LowLevelMultipartCopy { - - public static void main(String[] args) throws IOException { - Regions clientRegion = Regions.DEFAULT_REGION; - String sourceBucketName = "*** Source bucket name ***"; - String sourceObjectKey = "*** Source object key ***"; - String destBucketName = "*** Target bucket name ***"; - String destObjectKey = "*** Target object key ***"; - - try { - AmazonS3 s3Client = AmazonS3ClientBuilder.standard() - .withCredentials(new ProfileCredentialsProvider()) - .withRegion(clientRegion) - .build(); - - // Initiate the multipart upload. - InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(destBucketName, - destObjectKey); - InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initRequest); - - // Get the object size to track the end of the copy operation. - GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceObjectKey); - ObjectMetadata metadataResult = s3Client.getObjectMetadata(metadataRequest); - long objectSize = metadataResult.getContentLength(); - - // Copy the object using 5 MB parts. - long partSize = 5 * 1024 * 1024; - long bytePosition = 0; - int partNum = 1; - List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>(); - while (bytePosition < objectSize) { - // The last part might be smaller than partSize, so check to make sure - // that lastByte isn't beyond the end of the object. - long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1); - - // Copy this part. - CopyPartRequest copyRequest = new CopyPartRequest() - .withSourceBucketName(sourceBucketName) - .withSourceKey(sourceObjectKey) - .withDestinationBucketName(destBucketName) - .withDestinationKey(destObjectKey) - .withUploadId(initResult.getUploadId()) - .withFirstByte(bytePosition) - .withLastByte(lastByte) - .withPartNumber(partNum++); - copyResponses.add(s3Client.copyPart(copyRequest)); - bytePosition += partSize; - } - - // Complete the upload request to concatenate all uploaded parts and make the - // copied object available. - CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest( - destBucketName, - destObjectKey, - initResult.getUploadId(), - getETags(copyResponses)); - s3Client.completeMultipartUpload(completeRequest); - System.out.println("Multipart copy complete."); - } catch (AmazonServiceException e) { - // The call was transmitted successfully, but Amazon S3 couldn't process - // it, so it returned an error response. - e.printStackTrace(); - } catch (SdkClientException e) { - // Amazon S3 couldn't be contacted for a response, or the client - // couldn't parse the response from Amazon S3. - e.printStackTrace(); - } - } - - // This is a helper function to construct a list of ETags. - private static List<PartETag> getETags(List<CopyPartResult> responses) { - List<PartETag> etags = new ArrayList<PartETag>(); - for (CopyPartResult response : responses) { - etags.add(new PartETag(response.getPartNumber(), response.getETag())); - } - return etags; - } - } - - +For examples of how to copy objects using multipart upload with the AWS SDK for Java, see [Copy part of an object from another object](https://docs.aws.amazon.com/AmazonS3/latest/API/s3_example_s3_UploadPartCopy_section.html) in the _Amazon S3 API Reference_.