Java SDK
    • PDF

    Java SDK

    • PDF

    Article Summary

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

    openstack4j 라이브러리를 이용하여 네이버 클라우드 플랫폼 Archive Storage를 사용하는 예제입니다. 이 가이드에서는 openstack4j 3.1.0 버전을 기준으로 설명합니다. 관련 문서는 openstack4j를 참조해 주십시오.

    설치

    라이브러리를 직접 다운로드하거나 Apache Maven으로 설치할 수 있습니다.

    • 라이브러리 다운로드
      openstack4j 3.1.0 버전 다운로드

    • Apache Maven으로 설치

      <dependency>
        <groupId>org.pacesys</groupId>
        <artifactId>openstack4j</artifactId>
        <version>3.1.0</version>
      </dependency>
      

    예제

    예제에서 사용하는 user, password에 적용할 값은 네이버 클라우드 플랫폼 포털에서 생성한 API 인증키 정보를 사용합니다. API 인증키 정보에서 Access Key ID를 user의 값에, Secret Key를 password의 값에 입력합니다. domainid, projectid는 Archive Storage 화면의 [API 이용 정보 확인] 버튼을 클릭하면 나타나는 팝업 창의 정보를 사용합니다. Domain ID를 domainid의 값에, Project ID의 값을 projectid의 값에 입력합니다.

    참고

    클라이언트 생성

    인증 정보를 통해 생성된 토큰 객체로 OS 클라이언트를 생성합니다. 클라이언트에서 토큰이 유효하지 않거나 만료되면 자동으로 토큰을 갱신합니다. 멀티 쓰레드 환경에서 토큰 객체를 공유하므로 불필요한 인증 절차를 줄일 수 있습니다.

    참고

    이 가이드에서 설명하는 모든 예제는 인증 정보를 통해 생성한 토큰 객체를 이용합니다.

    final String endpoint = "https://kr.archive.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);
    
    
    // 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 :)
        }
    }
    
    

    컨테이너(버킷) 목록 조회

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

    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);
    }
    
    

    컨테이너(버킷) 생성

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

    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);
    
    

    컨테이너(버킷) 삭제

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

    OSClientV3 client = OSFactory.clientFromToken(token);
    ObjectStorageContainerService containerService = client.objectStorage().containers();
    
    String containerName = "sample-container";
    
    ActionResponse response = containerService.delete(containerName);
    System.out.println(response);
    
    

    오브젝트 업로드

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

    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);
    
    

    디렉터리 오브젝트 생성

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

    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);
    
    

    오브젝트 목록 조회

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

    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);
    }
    
    

    오브젝트 다운로드

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

    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);
    
    

    오브젝트 삭제

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

    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);
    
    

    대용량 파일 업로드(SLO)

    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);
    
    

    대용량 파일 Segments 조회(SLO)

    대용량 파일 Segments를 조회하는 예시는 다음과 같습니다.

    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);
    }
    
    

    대용량 파일 삭제(SLO)

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

    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);
    
    

    이 문서가 도움이 되었습니까?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.