-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathCopyObjectExample.java
154 lines (136 loc) · 7.21 KB
/
CopyObjectExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.CopyObjectDetails;
import com.oracle.bmc.objectstorage.model.CreateBucketDetails;
import com.oracle.bmc.objectstorage.model.WorkRequest;
import com.oracle.bmc.objectstorage.requests.CopyObjectRequest;
import com.oracle.bmc.objectstorage.requests.CreateBucketRequest;
import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest;
import com.oracle.bmc.objectstorage.requests.GetWorkRequestRequest;
import com.oracle.bmc.objectstorage.requests.HeadObjectRequest;
import com.oracle.bmc.objectstorage.requests.PutObjectRequest;
import com.oracle.bmc.objectstorage.responses.CopyObjectResponse;
import com.oracle.bmc.objectstorage.responses.GetNamespaceResponse;
import com.oracle.bmc.objectstorage.responses.GetWorkRequestResponse;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
public class CopyObjectExample {
/**
* The entry point for the example.
*
* @param args Arguments to provide to the example. The following arguments are expected:
* <ul>
* <li>The first argument is the OCID of the compartment.
* <li>The second is the name of source bucket to create
* <li>The third is the name of source object to copy from
* <li>The fourth is the name of destination bucket to create
* <li>The fifth is the name of destination object to copy to
* </ul>
*/
public static void main(String[] args) throws Exception {
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
if (args.length != 5) {
throw new IllegalArgumentException(
"Unexpected number of arguments received. Consult the script header comments for expected arguments");
}
final String compartmentId = args[0];
final String sourceBucket = args[1];
final String sourceObject = args[2];
final String destBucket = args[3];
final String destObject = args[4];
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI
// config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to
// the following
// line if needed and use ConfigFileReader.parse(CONFIG_LOCATION, CONFIG_PROFILE);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
ObjectStorage client = ObjectStorageClient.builder().build(provider);
client.setRegion(Region.US_PHOENIX_1);
System.out.println("Getting the namespace.");
GetNamespaceResponse namespaceResponse =
client.getNamespace(GetNamespaceRequest.builder().build());
String namespaceName = namespaceResponse.getValue();
System.out.println("Creating the source bucket.");
CreateBucketDetails createSourceBucketDetails =
CreateBucketDetails.builder()
.compartmentId(compartmentId)
.name(sourceBucket)
.build();
CreateBucketRequest createSourceBucketRequest =
CreateBucketRequest.builder()
.namespaceName(namespaceName)
.createBucketDetails(createSourceBucketDetails)
.build();
client.createBucket(createSourceBucketRequest);
System.out.println("Creating the source object");
PutObjectRequest putObjectRequest =
PutObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(sourceBucket)
.objectName(sourceObject)
.contentLength(4L)
.putObjectBody(
new ByteArrayInputStream("data".getBytes(StandardCharsets.UTF_8)))
.build();
client.putObject(putObjectRequest);
System.out.println("Creating the destination bucket.");
client.setRegion(Region.US_ASHBURN_1);
CreateBucketDetails createDestBucketDetails =
CreateBucketDetails.builder().compartmentId(compartmentId).name(destBucket).build();
CreateBucketRequest createDestBucketRequest =
CreateBucketRequest.builder()
.namespaceName(namespaceName)
.createBucketDetails(createDestBucketDetails)
.build();
client.createBucket(createDestBucketRequest);
System.out.println("Copy the object.");
client.setRegion(Region.US_PHOENIX_1);
CopyObjectDetails copyObjectDetails =
CopyObjectDetails.builder()
.sourceObjectName(sourceObject)
.destinationRegion(Region.US_ASHBURN_1.getRegionId())
.destinationNamespace(namespaceName)
.destinationBucket(destBucket)
.destinationObjectName(destObject)
.build();
CopyObjectRequest copyObjectRequest =
CopyObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(sourceBucket)
.copyObjectDetails(copyObjectDetails)
.build();
CopyObjectResponse copyObjectResponse = client.copyObject(copyObjectRequest);
System.out.println("Wait for copy to finish.");
GetWorkRequestRequest getWorkRequestRequest =
GetWorkRequestRequest.builder()
.workRequestId(copyObjectResponse.getOpcWorkRequestId())
.build();
GetWorkRequestResponse getWorkRequestResponse =
client.getWaiters().forWorkRequest(getWorkRequestRequest).execute();
WorkRequest.Status status = getWorkRequestResponse.getWorkRequest().getStatus();
System.out.println("Work request is now in " + status + " state.");
if (status == WorkRequest.Status.Completed) {
System.out.println("Verify that the object has been copied.");
client.setRegion(Region.US_ASHBURN_1);
HeadObjectRequest headObjectRequest =
HeadObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(destBucket)
.objectName(destObject)
.build();
client.headObject(headObjectRequest);
}
client.close();
}
}