Classic/VPC環境で利用できます。
OpenStackが提供する Javascript SDKを利用して NAVERクラウドプラットフォームの Archive Storageを利用する場合の例です。本ガイドは openstack-swift-client 2.1.0バージョンを基準に説明します。
インストール
SDKをインストールする方法は次の通りです。
npm install --save openstack-swift-client@2.1.0
参考
python-swiftclientのソース情報とドキュメントは次の通りです。
ユースケース
例で使う username、passwordに適用する値は NAVERクラウドプラットフォームポータルで作成した 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}`);
})();