Classic/VPC 환경에서 이용 가능합니다.
OpenStack에서 제공하는 Javascript SDK를 이용하여 네이버 클라우드 플랫폼의 Archive Storage를 사용하는 예제입니다. 이 가이드에서는 openstack-swift-client 2.1.0 버전을 기준으로 안내합니다.
설치
SDK를 설치하는 방법은 다음과 같습니다.
npm install --save openstack-swift-client@2.1.0
참고
openstack-swift-client 2.1.0 버전의 관련 문서는 다음과 같습니다.
예제
Javascript SDK를 이용하여 Archive Storage를 사용하는 방법을 설명합니다.
참고
예제에 사용된 ACCESS_KEY
, SECRET_KEY
, DOMAIN_ID
, PROJECT_ID
는 네이버 클라우드 플랫폼에서 발급받은 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));
})();
대용량 파일 업로드(SLO)
SLO 방식을 사용하여 대용량 파일을 업로드하는 예시는 다음과 같습니다.
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}`);
})();