Javascript SDK
    • PDF

    Javascript SDK

    • PDF

    기사 요약

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

    Javascript SDK for Swift API

    OpenStack에서 제공하는 Javascript SDK를 이용하여 네이버 클라우드 플랫폼[공공기관용] Archive Storage를 사용하는 방법을 설명합니다.

    이 문서는 openstack-swift-client 2.1.0 버전을 기준으로 작성되었습니다.

    설치

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

    python-swiftclient

    예제

    예제에서 사용하는 user, key는 포탈 마이페이지 > 계정 관리 > 인증키 관리 에서 생성한 API 인증키를 사용합니다. (Access Key ID는 username, Secret Key는 password)
    projectId와 domainId 정보는 Archive Storage 콘솔에서 이용 신청 후 [API 이용 정보 확인] 버튼을 통해 확인할 수 있습니다.

    컨테이너(버킷) 생성

    const SwiftClient = require('openstack-swift-client');
    
    let credentials = {
      endpointUrl: 'https://kr.archive.gov-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.gov-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.gov-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.gov-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.gov-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.gov-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.gov-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.gov-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));
        
    })();
    

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

    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.