1. API Key생성하기
아래 링크참조
2. Java SDK로 Object Storage Copy하기
> Maven Dependency
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-objectstorage</artifactId>
</dependency>
> Java Code
// Object Storage를 서울Region -> 춘천Region으로 Copy하기
final String compartmentId = "ocid1.compartment.~~~~~~~~~~mkl6ezjjtc7idlpg54rqa";
final String sourceBucket = "bucket-soruce";
final String sourceObject = "test.text";
final String destBucket = "bucket-copy";
final String destObject = "test.text";
// privider가져오는 코드 생략, 위 API Key가져오기 Link참조
ObjectStorage client = new ObjectStorageClient(provider);
client.setRegion(Region.AP_SEOUL_1);
System.out.println("Getting the namespace.");
// Each Oracle Cloud Infrastructure tenant is assigned one unique and uneditable Object Storage 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.AP_CHUNCHEON_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.AP_SEOUL_1);
CopyObjectDetails copyObjectDetails =
CopyObjectDetails.builder()
.sourceObjectName(sourceObject)
.destinationRegion(Region.AP_CHUNCHEON_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.AP_CHUNCHEON_1);
HeadObjectRequest headObjectRequest =
HeadObjectRequest.builder()
.namespaceName(namespaceName)
.bucketName(destBucket)
.objectName(destObject)
.build();
client.headObject(headObjectRequest);
}
client.close();
위 Git에서 CopyObjectExample.java 참고
작성자: 손창호(primelyson@gmail.com) / Cloud Engineer, 정보시스템감리사, DAP, PIA
개인의 시간을 할애하여 작성된 글로서 글의 내용에 오류가 있을 수 있으며, 글 속의 의견은 개인적인 의견입니다..
'9. 실습 Hands-on Lab' 카테고리의 다른 글
스크립트로 실행 자원 자동 멈춤 - Auto Stop (0) | 2023.09.18 |
---|---|
OCI Super Delete 스크립트로 Compartment와 자원 삭제 (0) | 2023.08.11 |
닷넷 코어 애플리케이션 설치하고 ADB 연결하기 .NET Core applications (0) | 2022.10.10 |
Tomcat을 설치하고 DBCS와 연동하기 (0) | 2022.09.26 |
Object Storage 활용 2. S3 Browser를 OCI에 연결하기 (0) | 2022.09.15 |
댓글