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