Object Storage CLI
    • PDF

    Object Storage CLI

    • PDF

    Article Summary

    Classic/VPC環境で利用できます。

    Amazon S3 CLIと互換性のある Object Storage CLIの初期設定方法から、簡単な使用方法などを確認します。

    初期設定

    Object Storage CLIを使用するためのインストールと認証方法について説明します。

    インストール

    インストールコマンドは、次のとおりです。

    pip install awscli==1.15.85
    
    参考

    認証

    認証情報を設定する方法は、次のとおりです。

    ~$ aws configure
    AWS Access Key ID [****************leLy]: ACCESS_KEY_ID
    AWS Secret Access Key [None]: SECRET_KEY
    Default region name [None]: [Enter]
    Default output format [None]: [Enter]
    

    注意事項

    Roleを取得したサーバで使用する際の注意点について説明します。Roleを取得したサーバは、別途の configureなしで Object Storageコマンドを使用できます。
    Roleを取得したサーバで使用する際には、Metadata APIのうち認証キー照会 APIを使用します。ただし、この過程で timeoutが発生する可能性があるため、必要に応じて以下のコマンドで Metadata API timeoutを延長して使用できます。

    export AWS_METADATA_SERVICE_TIMEOUT=10
    

    CLIを利用する場合 --endpoint-url オプションは必須です。リージョン別呼び出しドメインは Object Storage APIガイドをご参照ください。

    コマンドのヘルプ

    Object Storage CLIを円滑に使用するためにヘルプを確認するには、以下のコマンドを実行します。

    aws help
    aws <command> help
    aws <command> <subcommand> help
    

    コマンド

    Object Storage CLIについて説明します。

    バケット作成

    バケットを作成するコマンドは、次のとおりです。

    • high-level

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 mb s3://<bucket_name>
      
    • API-level

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api create-bucket --bucket <bucket_name>
      
      参考

      awscli 2バージョンの場合、バケット作成コマンド(mb)を実行するには、--region us-east-1 オプションが必要です。

    バケットリストの照会

    バケットリストを照会するコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 ls
      
    • API-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api list-buckets
      

    オブジェクトリストの照会

    すべてのオブジェクトリストの照会

    すべてのオブジェクトリストを照会するコマンドは、次のとおりです。

    • high-level

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 ls s3://<bucket_name>
      
    • API-level

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api list-objects --bucket <bucket_name>
      

    特定の文字列を含むオブジェクトの照会

    オブジェクト名に特定の文字列(例: foo)を含むオブジェクトリストを照会するコマンドは、次のとおりです。

    • API-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api list-objects --bucket <bucket_name> --prefix <prefix> --query "Contents[?contains(Key, 'foo')]"
      

    サイズの照会

    バケットサイズの照会

    バケットサイズを照会するためのオプションは、high-levelコマンドでのみ実行できます。バケットサイズを照会するコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 ls s3://<bucket_name> --recursive --human-readable --summarize
      

    フォルダサイズの照会

    フォルダサイズを照会するためのオプションは、high-levelコマンドでのみ実行できます。フォルダサイズを照会するコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 ls s3://<bucket_name>/<folder_name> --recursive --human-readable --summarize
      

    オブジェクトのアップロード

    オブジェクトをアップロードするコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 cp <local_file_path> s3://<bucket_name>[/<object_name>]
      
    • API-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api put-object --bucket <bucket_name> --key <object_name> --body <local_file_path>
      

    マルチパートアップロード

    順番に進めることで、正常にマルチパートアップロードができます。

    1. マルチパートアップロードの開始

    以下のコマンドを入力して UploadId値を作成して返します。

    aws --endpoint-url=https://kr.object.ncloudstorage.com s3api create-multipart-upload --bucket <bucket_name> --key <object_name>
    
    1. マルチパートアップロード

    以下のコマンドを入力してパートをアップロードし、アップロードしたパートの ETag値を返します。

    aws --endpoint-url=https://kr.object.ncloudstorage.com s3api upload-part --bucket <bucket_name> --key <object_name> --part-number <sequential_integer> --body <local_file_path> --upload-id <upload_id>
    
    1. マルチパートアップロードの完了

    以下のコマンドを入力して、上記でアップロードしたパートのマルチパートアップロードを完了します。

    aws --endpoint-url=https://kr.object.ncloudstorage.com s3api complete-multipart-upload --bucket <bucket_name> --key <object_name> --upload-id <upload_id> --multipart-upload file://<json_file>
    
    • json_fileのユースケース:
    {
      "Parts": [
        {"ETag": "90e6f8b09862bb7fdc080c89d53dbc65", "PartNumber": 1},
        {"ETag": "f158dfe47ee46ce22a80986cf8f4bdbb", "PartNumber": 2},
         ......
        {"ETag": "7d29c5f2bc97aebc6a395b52a0fb9d88", "PartNumber": 7}
      ]
    }
    

    オブジェクトのダウンロード

    オブジェクトをダウンロードするコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=http://kr.object.ncloudstorage.com s3 cp s3://<bucket_name>/<object_name> <local_file_path>
      
    • API-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api get-object --bucket <bucket_name> --key <object_name> <local_file_path>
      

    オブジェクトの削除

    オブジェクトを削除するコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 rm s3://<bucket_name>/<object_name>
      
    • API-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api delete-object --bucket <bucket_name> --key <object_name>
      

    バケット/フォルダ内のすべてのオブジェクトの削除

    バケットまたはフォルダ内のすべてのオブジェクトを削除するコマンドは、次のとおりです。

    • high-level
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 rm s3://<bucket_name>/[<folder_name>/] --recursive
      

    未完了のマルチパートアップロードの削除

    未完了のマルチパートアップロードがあるか確認し、未完了のマルチパートアップロードを削除する方法は、次のとおりです。

    1. 未完了のマルチパートアップロードの照会

    以下のコマンドで未完了のマルチパートアップロード情報を照会します。

    aws --endpoint-url=https://kr.object.ncloudstorage.com s3api list-multipart-uploads --bucket <bucket_name>
    
    1. 未完了のマルチパートアップロードの削除

    以下のコマンドで上記で照会した upload_id を入力し、未完了のマルチパートアップロードを削除できます。

    aws --endpoint-url=https://kr.object.ncloudstorage.com s3api abort-multipart-uploads --bucket <bucket_name> --key <object_name> --upload-id <upload_id>
    

    バケット削除

    バケットを削除するコマンドは、次のとおりです。

    • high-level

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 rb s3://<bucket_name>
      

      forceオプションを追加すると、空でないバケットを削除できます。バケットに属しているオブジェクトをすべて削除してからバケットを削除します。

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 rb s3://<bucket_name> --force
      
    • API-level

      aws --endpoint-url=https://kr.object.ncloudstorage.com s3api delete-bucket --bucket <bucket_name>
      
      参考

      API-levelの場合、空でないバケットは削除できません。

    同期

    同期機能は、high-levelコマンドでのみ実行できます。 --delete オプションを使用すると、sourceにないファイルは destinationで削除します。同期コマンドは、次のとおりです。

    • high-level
      • Object Storageのバケット(フォルダ)のすべてのファイルをローカルに同期

        aws --endpoint-url=https://kr.object.ncloudstorage.com s3 sync s3://<bucket_name>[/<object_name>] <local_directory_name>
        
      • ローカルディレクトリのすべてのファイルを Object Storageのバケット(フォルダ)に同期

        aws --endpoint-url=https://kr.object.ncloudstorage.com s3 sync <local_directory_name> s3://<bucket_name>[/<object_name>]
        
      • Object Storageのバケット(フォルダ)のすべてのファイルを他のバケット(フォルダ)に同期

        aws --endpoint-url=https://kr.object.ncloudstorage.com s3 sync s3://<source_bucket_name>[/<destination_object_name>] s3://<destination_bucket_name>[/<destination_object_name>]
        
        参考

        awscli 2バージョンの場合、同期コマンドの実行時にサポートされていない apiを呼び出しエラーが発生する可能性があるため、1.15.xバージョンの使用をお勧めします。

    アクセス制御リスト(ACL)

    Object Storageバケット(フォルダ)のファイルに対するアクセス制御リスト(ACL)権限が設定できます。アクセス制御リスト機能は、API-levelコマンドでのみ実行できます。コマンドは、次のとおりです。

    • API-level
      • Object Storageバケットのファイルを公開に設定
        aws --endpoint-url=https://kr.object.ncloudstorage.com s3api put-object-acl --bucket <bucket_name> --key <object_name> --acl public-read
        
      • Object Storageバケットのファイルを公開しないに設定
        aws --endpoint-url=https://kr.object.ncloudstorage.com s3api put-object-acl --bucket <bucket_name> --key <object_name> --acl private
        

    リスト照会コマンド(ls)と一緒に使用すると、Object Storageバケット(フォルダ)にアップロードされたすべてのファイルを公開に設定できます。コマンドは、次のとおりです。

    • Object Storageバケットのすべてのファイルを公開に設定

      注意

      バケットのすべてのファイルが公開に設定されますので、コマンド実行時はご注意ください。

      aws --endpoint-url=https://kr.object.ncloudstorage.com/ s3 ls s3://<bucket_name> --recursive | awk '{cmd="aws --endpoint-url=https://kr.object.ncloudstorage.com/ s3api put-object-acl --bucket <bucket_name> --acl public-read --key "$4; system(cmd)}'
      

    デバッグ

    デバッグ関連コマンドは、次のとおりです。

    • --debug*オプションを使用して詳細ログを確認
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 cp <local_file_path> s3://<bucket_name>[/<object_name>] --debug
      
    • ログファイルに保存
      aws --endpoint-url=https://kr.object.ncloudstorage.com s3 cp <local_file_path> s3://<bucket_name>[/<object_name>] --debug 2> debug.log
      

    CORS設定

    Object Storageバケットに対して、CORS(Cross-Origin Resource Sharing)を設定します。Object Storageバケットで交差ソースリクエストを受信すると、CORS構成を確認してブラウザリクエストと一致する最初の CORSルールを使用して交差ソースリクエストを許可します。CORSルールと一致するには、次の事項を満たす必要があります。

    • リクエストの Originヘッダは、AllowedOrigins要素と一致する必要があります。
    • リクエスト methodまたは Access-Control-Request-Methodヘッダ(Preflightリクエスト)は、AllowedMethods要素と一致する必要があります。
    • Access-Control-Request-Headersヘッダ(Preflightリクエスト)に指定されたすべてのヘッダは、AllowedHeaders要素と一致する必要があります。CORS設定は、API-levelコマンドでのみ実行できます。

    コマンドは、次のとおりです。

    • API-level
      • jsonファイルで定義した CORSルールを Object Storageバケットに設定
        aws --endpoint-url=https://kr.object.ncloudstorage.com s3api put-bucket-cors --bucket <bucket_name> --cors-configuration file://cors.json
        
        cors.json:
        {
          "CORSRules": [
          {
              "AllowedHeaders": ["*"],
              "AllowedMethods": ["GET","PUT"],
              "AllowedOrigins": ["*"],
              "MaxAgeSeconds": 3000
            }
          ]
        }
        
      • Object Storageバケットに設定された CORSルールを照会
        aws --endpoint-url=https://kr.object.ncloudstorage.com s3api get-bucket-cors --bucket <bucket_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.