Javascript SDK
    • PDF

    Javascript SDK

    • PDF

    Article Summary

    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ソース情報およびドキュメントは以下のとおりです。

    例で使用する usernamepasswordに適用する値は NAVERクラウドプラットフォームポータルで作成した API認証キー情報を使用します。API認証キー情報で Access Key IDを usernameの値に、Secret Keyを passwordの値に入力します。domainidprojectidは Archive Storage画面の [API利用情報の確認] ボタンをクリックし、表示されるポップアップウィンドウの情報を使用します。Domain IDを domainidの値に、Project IDの値を projectidの値に入力します。

    参考

    コンテナ(バケット)の作成

    コンテナ(バケット)を作成する例は以下のとおりです。

    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));
        
    })();
    

    この記事は役に立ちましたか?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.