- Print
- PDF
Javascript SDK
- Print
- PDF
Available in Classic and VPC.
This is an example of using NAVER Cloud Platform’s Archive Storage using Javascript SDK provided by OpenStack. This guide is based on openstack-swift-client 2.1.0 version.
Installation
The following describes how to install an SDK.
npm install --save openstack-swift-client@2.1.0
Here is the python-swiftclient source information and documentation.
Example
The valuesto be applied to username
, password
, in the example use the API authentication key information created on the NAVER Cloud Platform portal. In the API authentication key information, enter the Access Key ID in the value of username
and the Secret Key in the value of password
. domainid
, projectid
use the information in the pop-up window that appears when you click the [Check API usage information] button on the Archive Storage screen. Enter the domain ID in the value of domainid
and the project ID value in the value of projectid
.
- For information on how to check the API authentication key information, see Create and Check API Authentication Key.
- To check the projectid and domainid information, please check API Use Information.
Create a container (bucket)
Here's an example of how you can create a container (bucket).
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);
})();
View container (bucket) list
Here's an example of how you can view a container (bucket).
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}`);
}
})();
Delete container (bucket)
Here's an example of how you can delete a container (bucket).
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);
})();
Create directory object
Here's an example of how you can create a directory object.
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)
})();
Upload object
Here's an example of how you can upload an object.
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);
})();
View object list
Here's an example of how you can view the object list.
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}`);
}
})();
Download object
Here's an example of how you can download an object.
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);
})();
Delete object
Here's an example of how you can delete an object.
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));
})();