Java SDK
    • PDF

    Java SDK

    • PDF

    Article Summary

    Available in Classic and VPC.

    This is an example of using NAVER Cloud Platform Archive Storage using openstack4j library. This guide is based on openstack4j 3.1.0 version. See openstack4j for related documentation.

    Installation

    You can download the library directly or install it with Apache Maven.

    • Library download openstack4j 3.1.0 version download

    • Install with Apache Maven

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

    Example

    The values​to be applied to user, password, in the example use the API authentication key information created on the NAVER Cloud Platform portal. In the API authentication key information, enter the Access Key ID in the value of user and the Secret Key in the value of password. domainid, projectid use the information in the pop-up window that appears when you click the [Check API usage information] button on the Archive Storage screen. Enter the domain ID in the value of domainid and the project ID value in the value of projectid.

    Note

    Create Client

    Create an OS client with a token object created with authentication information. The client automatically renews the token when it becomes invalid or expires. Since token objects are shared in a multi-threaded environment, unnecessary authentication procedures can be reduced.

    Note

    All examples in this guide use a token object created with credentials.

    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()
        .token;
    
    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 :)
        }
    }
    
    

    View container (bucket) list

    Here's an example of how you can view a list of containers (buckets).

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

    Create a container (bucket)

    Here's an example of how you can create a container (bucket).

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

    Delete container (bucket)

    Here's an example of how you can delete a container (bucket).

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

    Upload object

    Here's an example of how you can upload an object.

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

    Create directory object

    Here's an example of how you can create a directory object.

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

    View object list

    Here's an example of how you can view the object list.

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

    Download object

    Here's an example of how you can download an 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);
    
    

    Delete object

    Here's an example of how you can delete an object.

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

    Uploading a large file(SLO)

    Here's an example of how you can upload an object using the SLO method.

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

    View large file segments (SLO)

    Here's an example of how you can view large file 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);
    }
    
    

    Delete large file (SLO)

    Here's an example of how you can delete large files.

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

    Was this article helpful?

    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.