Available in Classic and VPC
This is an example of using NAVER Cloud Platform's Archive Storage with the JavaScript SDK provided by OpenStack. This guide is based on the openstack-swift-client version 2.1.0.
Installation
To install SDK:
npm install --save openstack-swift-client@2.1.0
The following is the source information and documentation for python-swiftclient.
Example
The values for the username and password used in this example are the API authentication key information generated in NAVER Cloud Platform portal. Enter the Access Key ID in the username field and the Secret Key in the password field. The domainid and projectid values are obtained from the popup window that appears when you click [Check API usage information] on the Archive Storage screen. Enter the Domain ID in the domainid field and the Project ID in the projectid field.
- For more information on how to verify API authentication key information, see Create and verify API authentication key.
- For more information on how to check projectid and domainid information, see Check API usage information.
Create a container (bucket).
This is an example of creating 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
This is an example of viewing 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)
This is an example of deleting 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
This is an example of creating 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
This is an example of uploading 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
This is an example of viewing an 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
This is an example of downloading 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.
This is an example of deleting 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));
})();
Upload large objects
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}`);
})();