Javascript SDK for Swift API
OpenStackで提供するJavascript SDKを利用して、NAVERクラウドプラットフォームのArchive Storageを使用する方法を説明します。
このテキストは、openstack-swift-client 2.1.0バージョンに基づいて作成されました。
インストール
npm install --save openstack-swift-client@2.1.0
python-swiftclient
- ソース : https://github.com/stefanodefaria/swift-client
- テキスト : https://github.com/stefanodefaria/swift-client
例
例で使用される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.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));
})();