Javascript SDK
- 인쇄
- PDF
Javascript SDK
- 인쇄
- PDF
기사 요약
이 요약이 도움이 되었나요?
의견을 보내 주셔서 감사합니다.
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의 값에 입력합니다.
참고
- API 인증키 정보를 확인하는 방법은 API 인증키 생성 및 확인을 참조해 주십시오.
- projectid와 domainid 정보를 확인하는 방법은 API 이용 정보 확인을 확인해 주십시오.
컨테이너(버킷) 생성
컨테이너(버킷)을 생성하는 예시는 다음과 같습니다.
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));
})();
대용량 오브젝트 업로드
const SwiftClient = require('openstack-swift-client');
const fs = require('fs');
const crypto = require('crypto');
const { Readable } = require('stream');
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 = '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}`);
})();
이 문서가 도움이 되었습니까?