Path: blob/master/code_examples/java_examples/S3Examples/CopyObjectSingleOperation.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.model.CopyObjectRequest;1112public class CopyObjectSingleOperation {1314public static void main(String[] args) throws IOException {15String clientRegion = "*** Client region ***";16String bucketName = "*** Bucket name ***";17String sourceKey = "*** Source object key *** ";18String destinationKey = "*** Destination object key ***";1920try {21AmazonS3 s3Client = AmazonS3ClientBuilder.standard()22.withCredentials(new ProfileCredentialsProvider())23.withRegion(clientRegion)24.build();2526// Copy the object into a new object in the same bucket.27CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, sourceKey, bucketName, destinationKey);28s3Client.copyObject(copyObjRequest);29}30catch(AmazonServiceException e) {31// The call was transmitted successfully, but Amazon S3 couldn't process32// it, so it returned an error response.33e.printStackTrace();34}35catch(SdkClientException e) {36// Amazon S3 couldn't be contacted for a response, or the client37// couldn't parse the response from Amazon S3.38e.printStackTrace();39}40}41}4243