AWS CLI の使用
AWS の公式コマンド・ライン・インターフェースは、IBM Cloud® Object Storage S3 API と互換性があります。
Python で作成されており、pip install awscli
を使用して Python Package Index からインストールできます。 デフォルトでは、アクセス・キーのソースは ~/.aws/credentials
ですが、環境変数として設定することもできます。
以下の例は、バージョン 1.14.2 の CLI を使用して生成されました。 インストールされているバージョンを確認するには、aws --version
を実行します。
Object Storage に接続するための CLI の構成
AWS CLI を構成するには、aws configure
と入力します。 HMAC 資格情報とデフォルトのリージョン名を指定します。 AWS S3 で使用される「リージョン名」は、Object Storage がストレージ・クラスを定義するために使用するコード (LocationConstraint
)
に対応しています。
LocationConstraint
の有効なコードのリストについては、ストレージ・クラス・ガイドを参照してください。
aws configure
AWS Access Key ID [None]: {Access Key ID}
AWS Secret Access Key [None]: {Secret Access Key}
Default region name [None]: {Provisioning Code}
Default output format [None]: json
これにより、以下の 2 つのファイルが作成されます。
~/.aws/credentials
:
[default]
aws_access_key_id = {Access Key ID}
aws_secret_access_key = {Secret Access Key}
~/.aws/config
:
[default]
region = {Provisioning Code}
output = json
また、以下のように環境変数を使用して HMAC 資格情報を設定することもできます。
export AWS_ACCESS_KEY_ID="{Access Key ID}"
export AWS_SECRET_ACCESS_KEY="{Secret Access Key}"
IBM COS エンドポイントのソースは、--endpoint-url
オプションを使用して指定する必要があり、資格情報ファイルで設定することはできません。
高水準の構文コマンド
単純なユース・ケースは、 aws --endpoint-url {endpoint} s3 <command>
を使用して実行できます。 エンドポイントについて詳しくは、エンドポイントおよびストレージ・ロケーションを参照してください。 オブジェクトは、ls
、mv
、cp
、rm
などの使い慣れたシェル・コマンドを使用して管理します。 バケットは mb
を使用して作成でき、rb
を使用して削除できます。
サービス・インスタンス内のすべてのバケットのリスト表示
aws --endpoint-url {endpoint} s3 ls
2016-09-09 12:48 s3://bucket-1
2016-09-16 21:29 s3://bucket-2
バケット内のオブジェクトのリスト表示
aws --endpoint-url {endpoint} s3 ls s3://bucket-1
2016-09-28 15:36 837 s3://bucket-1/c1ca2-filename-00001
2016-09-09 12:49 533 s3://bucket-1/c9872-filename-00002
2016-09-28 15:36 14476 s3://bucket-1/98837-filename-00003
2016-09-29 16:24 20950 s3://bucket-1/abfc4-filename-00004
新規バケットの作成
注: 個人情報 (PII): バケットまたはオブジェクトに 名前を付ける 場合は、名前、場所、またはその他の方法でユーザー (個人) を識別できる情報を使用しないでください。
~/.aws/config
ファイル内のデフォルトのリージョンが、選択したエンドポイントと同じロケーションに対応している場合、バケットの作成は簡単です。
aws --endpoint-url {endpoint} s3 mb s3://bucket-1
make_bucket: s3://bucket-1/
バケットへのオブジェクトの追加
aws --endpoint-url {endpoint} s3 cp large-dataset.tar.gz s3://bucket-1
upload: ./large-dataset.tar.gz to s3://bucket-1/large-dataset.tar.gz
以下のように、ファイル名とは異なる新しいオブジェクト・キーを設定することもできます。
aws --endpoint-url {endpoint} s3 cp large-dataset.tar.gz s3://bucket-1/large-dataset-for-project-x
upload: ./large-dataset.tar.gz to s3://bucket-1/large-dataset-for-project-x
同じリージョン内のあるバケットから別のバケットへのオブジェクトのコピー:
$ aws --endpoint-url {endpoint} s3 cp s3://bucket-1/new-file s3://bucket-2/
copy: s3://bucket-1/new-file to s3://bucket-2/new-file
バケットからオブジェクトを削除します
aws --endpoint-url {endpoint} s3 rm s3://mybucket/argparse-1.2.1.tar.gz
delete: s3://mybucket/argparse-1.2.1.tar.gz
バケットの削除
aws --endpoint-url {endpoint} s3 rb s3://bucket-1
remove_bucket: s3://bucket-1/
事前署名 URL の作成
CLI は、事前署名 URL を作成できます。 これらの URL により、既存のアクセス制御を変更することなく、オブジェクトへの一時的なパブリック・アクセスが可能になります。
$ aws --endpoint-url {endpoint} s3 presign s3://bucket-1/new-file
以下のように、URL の存続時間を秒単位で設定することもできます (デフォルトは 3600)。
$ aws --endpoint-url {endpoint} s3 presign s3://bucket-1/new-file --expires-in 600
低水準の構文コマンド
AWS CLI では、s3api
コマンドを使用して、直接 HTTP 要求と同じ応答を提供する直接 API 呼び出しも可能です。
バケットのリスト表示:
$ aws --endpoint-url {endpoint} s3api list-buckets
{
"Owner": {
"DisplayName": "{storage-account-uuid}",
"ID": "{storage-account-uuid}"
},
"Buckets": [
{
"CreationDate": "2016-09-09T12:48:52.442Z",
"Name": "bucket-1"
},
{
"CreationDate": "2016-09-16T21:29:00.912Z",
"Name": "bucket-2"
}
]
}
バケット内のオブジェクトのリスト表示
$ aws --endpoint-url {endpoint} s3api list-objects --bucket bucket-1
{
"Contents": [
{
"LastModified": "2016-09-28T15:36:56.807Z",
"ETag": "\"13d567d518c650414c50a81805fff7f2\"",
"StorageClass": "STANDARD",
"Key": "c1ca2-filename-00001",
"Owner": {
"DisplayName": "{storage-account-uuid}",
"ID": "{storage-account-uuid}"
},
"Size": 837
},
{
"LastModified": "2016-09-09T12:49:58.018Z",
"ETag": "\"3ca744fa96cb95e92081708887f63de5\"",
"StorageClass": "STANDARD",
"Key": "c9872-filename-00002",
"Owner": {
"DisplayName": "{storage-account-uuid}",
"ID": "{storage-account-uuid}"
},
"Size": 533
},
{
"LastModified": "2016-09-28T15:36:17.573Z",
"ETag": "\"a54ed08bcb07c28f89f4b14ff54ce5b7\"",
"StorageClass": "STANDARD",
"Key": "98837-filename-00003",
"Owner": {
"DisplayName": "{storage-account-uuid}",
"ID": "{storage-account-uuid}"
},
"Size": 14476
},
{
"LastModified": "2016-10-06T14:46:26.923Z",
"ETag": "\"2bcc8ee6bc1e4b8cd2f9a1d61d817ed2\"",
"StorageClass": "STANDARD",
"Key": "abfc4-filename-00004",
"Owner": {
"DisplayName": "{storage-account-uuid}",
"ID": "{storage-account-uuid}"
},
"Size": 20950
}
]
}
静的 Web サイトの構成
aws --endpoint-url=https://<endpoint> s3 website s3://<bucketname>/ --index-document index.html --error-document error.html
次のステップ
IBM Cloud Object Storage の RESTful API の詳細な説明については、 S3 Compatibility API Documentation または Configuration API Documentationを参照してください。