Javascript SDK
    • PDF

    Javascript SDK

    • PDF

    Article Summary

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

    OpenStack에서 제공하는 Javascript SDK를 이용하여 네이버 클라우드 플랫폼 Archive Storage를 사용하는 예제입니다. 이 가이드는 openstack-swift-client 2.1.0 버전을 기준으로 설명합니다.

    설치

    SDK를 설치하는 방법은 다음과 같습니다.

    npm install --save openstack-swift-client@2.1.0
    
    참고

    python-swiftclient 소스 정보 및 문서는 다음과 같습니다.

    예제

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

    참고

    컨테이너(버킷) 생성

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

    const SwiftClient = require('openstack-swift-client');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    const container_name = 'sample-container';
    
    (async () => {
        await client.create(container_name);
    })();
    

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

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

    const SwiftClient = require('openstack-swift-client');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    (async () => {
        let response = await client.list();
        console.log('Container List');
        for(let container of response) {
            console.log(`
            > Count = ${container.count}
            > Last Modified = ${container.last_modified}
            > Name = ${container.name}
            > Bytes = ${container.bytes}`);
        }
    })();
    

    컨테이너(버킷) 삭제

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

    const SwiftClient = require('openstack-swift-client');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    
    const container_name = 'sample-container';
    
    (async () => {
        await client.delete(container_name);
    })();
    

    디렉터리 오브젝트 생성

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

    const SwiftClient = require('openstack-swift-client');
    const fs = require('fs');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    
    
    const container_name = 'sample-container';
    const object_name = 'sample-directory';
    const extra_header = {
        'Content-Type': 'application/directory'
    };
    
    (async () => {
        await client.create(`${container_name}/${object_name}/`, null, null, extra_header)
    })();
    

    오브젝트 업로드

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

    const SwiftClient = require('openstack-swift-client');
    const fs = require('fs');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    const container_name = 'sample-container';
    const object_name = 'sample-object';
    const local_file_name = '/tmp/test.txt';
    const extra_header = {
        'Content-Type': 'text/plain'
    };
    
    const container = client.container(container_name);
    
    (async () => {
        
        await container.create(object_name, fs.createReadStream(local_file_name), null, extra_header);
        
    })();
    

    오브젝트 목록 조회

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

    const SwiftClient = require('openstack-swift-client');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    const container_name = 'sample-container';
    const container = client.container(container_name);
    
    
    (async () => {
        
        let response = await container.list(container_name);
        
        for(let object of response) {
            console.log(`
            > Content Type = ${object.content_type}
            > Last Modified = ${object.last_modified}
            > Name = ${object.name}
            > Bytes = ${object.bytes}`);
        }
        
    })();
    

    오브젝트 다운로드

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

    const SwiftClient = require('openstack-swift-client');
    const fs = require('fs');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    
    const container_name = 'sample-container';
    const object_name = 'sample-object';
    const local_file_path = '/tmp/test';
    const container = client.container(container_name);
    
    
    
    (async () => {
        
        const stream = fs.createWriteStream(local_file_path);
    
        await container.get(encodeURIComponent(object_name), stream);
        
    })();
    

    오브젝트 삭제

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

    const SwiftClient = require('openstack-swift-client');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.ncloudstorage.com:5000/v3',
      username: 'ACCESS_KEY',
      password: 'SECRET_KEY',
      domainId: 'DOMAIN_ID',
      projectId: 'PROJECT_ID'
    };
    
    // swift client
    const client = new SwiftClient(
      new SwiftClient.KeystoneV3Authenticator(credentials)
    );
    
    const container_name = 'sample-container';
    const object_name = 'sample-object';
    const container = client.container(container_name);
    
    
    
    (async () => {
        
        await container.delete(encodeURIComponent(object_name));
        
    })();
    

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

    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.