Java SDK

Prev Next

Classic/VPC 환경에서 이용 가능합니다.

openstack4j 라이브러리를 이용하여 네이버 클라우드 플랫폼의 Archive Storage를 사용하는 예제입니다. 이 가이드에서는 openstack4j 3.1.0 버전을 기준으로 안내합니다.

참고

openstack4j 3.1.0 버전의 관련 문서는 다음과 같습니다.

SDK를 설치하는 방법은 다음과 같습니다.
라이브러리를 직접 다운로드하거나 Apache Maven으로 설치할 수 있습니다.

  • 라이브러리 다운로드: openstack4j 3.1.0 버전 다운로드
  • Apache Maven으로 설치
    <dependency>
    <groupId>org.pacesys</groupId>
    <artifactId>openstack4j</artifactId>
    <version>3.1.0</version>
    </dependency>
    
    Plain text

이 가이드의 모든 예제는 OSClientV3 객체를 사용합니다.
OSClientV3 객체는 인증 정보를 기반으로 생성된 Token 객체를 이용해 생성됩니다.
토큰이 만료되거나 유효하지 않은 경우 자동으로 갱신되며, 멀티스레드 환경에서는 하나의 토큰 객체를 공유하여 불필요한 인증 절차를 줄일 수 있습니다.

참고

예제에 사용된 ACCESS_KEY_ID, SECRET_KEY, DOMAIN_ID, PROJECT_ID는 네이버 클라우드 플랫폼에서 발급받은 API 인증키와 인증 정보 값입니다.
해당 값을 확인하는 방법은 서비스 연동을 참조해 주십시오.

final String endpoint = "https://kr.archive.gov-ncloudstorage.com:5000/v3";
final String user = "ACCESS_KEY_ID";
final String password = "SECRET_KEY";
final String domainId = "DOMAIN_ID";
final String projectId = "PROJECT_ID";

Token token = OSFactory.builderV3()
    .endpoint(endpoint)
    .credentials(user, password, Identifier.byId(domainId))
    .scopeToProject(Identifier.byId(projectId), Identifier.byId(domainId))
    .authenticate()
    .getToken();

OSClientV3 client = OSFactory.clientFromToken(token);

Java
// Spawn off a thread giving it the access
myThreadExecutor.submit(new MyRunnableOrCallable(token));

// Example of the Runnable or other object invoked in a new thread
public class MyRunnable implements Runnable {
     private OSClientV3 client;

     public MyRunnable(Access access) {
          client = OSFactory.clientFromToken(token);
     }

    public void run() {
        // can now use the client :)
    }
}

Java

Java SDK를 이용하여 Archive Storage를 사용하는 방법을 설명합니다.

컨테이너(버킷) 목록을 조회하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();

List<? extends SwiftContainer> containers = containerService.list(ContainerListOptions.create().limit(100));

for (SwiftContainer container : containers) {
    System.out.println(container);
}

Java

컨테이너(버킷)를 생성하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();

String containerName = "sample-container";

// with metadata
// X-Container-Meta-Test-Meta-Key: test-meta-value
Map<String, String> metadata = new HashMap<>();
metadata.put("test-meta-key", "test-meta-value");

ActionResponse response = containerService.create(containerName, CreateUpdateContainerOptions.create().metadata(metadata));
System.out.println(response);

Java

컨테이너(버킷)를 삭제하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();

String containerName = "sample-container";

ActionResponse response = containerService.delete(containerName);
System.out.println(response);

Java

오브젝트를 업로드하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String objectName = "sample-object.txt";
String contentType = "text/plain";

File file = new File("/tmp/sample-object.txt");

// with metadata
// X-Object-Meta-Test-Meta-Key : test-meta-value
Map<String, String> metadata = new HashMap<>();
metadata.put("test-meta-key", "test-meta-value");

String etag = objectService.put(containerName, objectName, Payloads.create(file),
    ObjectPutOptions.create().contentType(contentType).metadata(metadata));
System.out.println(etag);

Java

디렉터리 오브젝트를 생성하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageContainerService containerService = client.objectStorage().containers();

String containerName = "sample-container";
String directoryName = "sample-directory";

String etag = containerService.createPath(containerName, directoryName);
System.out.println(etag);

Java

오브젝트 목록을 조회하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String directoryName = "sample-directory";

// simple
List<? extends SwiftObject> objects = objectService.list(containerName);
for (SwiftObject object : objects) {
    System.out.println(object);
}

// list for directory
List<? extends SwiftObject> objectsForDirectory = objectService.list(containerName,
    ObjectListOptions.create().path(directoryName));
for (SwiftObject object : objectsForDirectory) {
    System.out.println(object);
}

Java

오브젝트를 다운로드하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String objectName = "sample-object.txt";
String downloadPath = "/tmp/sample-object.txt";

DLPayload payload = objectService.download(containerName, objectName);
File file = new File(downloadPath);
payload.writeToFile(file);

Java

오브젝트를 삭제하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String objectName = "sample-object.txt";

ActionResponse response = objectService.delete(containerName, objectName);
System.out.println(response);

Java

SLO 방식을 사용하여 대용량 파일을 업로드하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(getToken());
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String objectName = "sample-big-file.mp4";
String contentType = "video/mp4";

String segmentContainerName = "segment-container";
String segmentPrefix = "segments-for-sample-big-file.mp4/segment-";
List<Map<String, Object>> segmentInfoList = new ArrayList<>();

File file = new File("/tmp/sample.mp4");
FileInputStream fs = new FileInputStream(file);

int bufferSize = 1024 * 1024 * 10; // 10MB
byte[] buffer = new byte[bufferSize];

int segmentNo = 1;
int bytesRead;

while ((bytesRead = fs.read(buffer)) != -1) {
    Map<String, Object> segmentInfo = new HashMap<>();
    InputStreamPayload payload = new InputStreamPayload(new ByteArrayInputStream(buffer, 0, bytesRead));
    String etag = objectService.put(segmentContainerName, segmentPrefix + segmentNo, payload);

    segmentInfo.put("path", "/" + segmentContainerName + "/" + segmentPrefix + segmentNo);
    segmentInfo.put("etag", etag);
    segmentInfo.put("size_bytes", bytesRead);
    segmentInfoList.add(segmentInfo);

    segmentNo++;
}
fs.close();

String manifest = new ObjectMapper().writeValueAsString(segmentInfoList);

String etag = objectService.put(containerName, objectName,
    Payloads.create(new ByteArrayInputStream(manifest.getBytes())),
    ObjectPutOptions.create().queryParam("multipart-manifest", "put").contentType(contentType));

System.out.println(etag);

Java

대용량 파일의 세그먼트 목록을 조회하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String objectName = "sample-big-file.mp4";

ObjectListOptions options = ObjectListOptions.create();
options.getOptions().put("multipart-manifest", "get");

List<? extends SwiftObject> objects = objectService.list(containerName + "/" + objectName, options);
for (SwiftObject object : objects) {
    System.out.println(object);
}

Java

대용량 파일을 삭제하는 예시는 다음과 같습니다.

OSClientV3 client = OSFactory.clientFromToken(token);
ObjectStorageObjectService objectService = client.objectStorage().objects();

String containerName = "sample-container";
String objectName = "sample-big-file.mp4";

ActionResponse response = objectService.delete(ObjectLocation.create(containerName, objectName),
    ObjectDeleteOptions.create().queryParam("multipart-manifest", "delete"));

System.out.println(response);

Java