Javascript SDK

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

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

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

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

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

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

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}`);
    }
})();
JavaScript

컨테이너(버킷) 삭제

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

디렉토리 오브젝트 생성

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

오브젝트 업로드

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

오브젝트 목록 조회

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}`);
    }
    
})();
JavaScript

오브젝트 다운로드

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

오브젝트 삭제

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

대용량 오브젝트 업로드

const SwiftClient = require('openstack-swift-client');
const fs = require('fs');
const crypto = require('crypto');
const { Readable } = require('stream');

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 = 'large-file';
const local_file_name = '/tmp/large-file';

const segment_container_name = 'sample-container-segments';
const segment_prefix = `${object_name}/segment-`;
const segment_size = 10 * 1024 * 1024; // 10MB

const container = client.container(container_name);
const segment_container = client.container(segment_container_name);

async function bufferToStream(buffer) {
    return new Readable({
        read() {
            this.push(buffer);
            this.push(null);
        }
    });
}

(async () => {
    const fileStream = fs.createReadStream(local_file_name, { highWaterMark: segment_size });
    let segments = [];
    let index = 0;

    for await (const chunk of fileStream) {
        const segment_name = `${segment_prefix}-part-${index}`;
        const etag = crypto.createHash('md5').update(chunk).digest('hex');
        const stream = await bufferToStream(chunk);

        console.log(`segment upload: ${segment_name} (${chunk.length} bytes)`);

        await segment_container.create(segment_name, stream, null, null);

        segments.push({
            path: `/${segment_container_name}/${segment_name}`,
            etag: etag
        });

        index++;
    }

    console.log("All segments uploaded.");
    
    // Manifest
    const slo_manifest = JSON.stringify(segments);

    // upload manifest
    await container.create(`${object_name}?multipart-manifest=put`, await bufferToStream(slo_manifest), null, {
        'Content-Type': 'application/json'
    });

    console.log(`SLO upload: ${object_name}`);

})();
JavaScript