AWSコマンドラインインターフェース(AWS CLI)でS3を構築してみる
S3に関するAWS CLIには、高レベルコマンド(aws s3)と低レベルコマンド(aws s3api)がある。
低レベルコマンドがより細やかな処理が可能。
高レベルコマンド aws s3
$ aws s3 <Command> [<Arg> ...]
Command | 意味 |
---|---|
cp | オブジェクトのコピー |
ls | オブジェクトの一覧 |
mb | S3バケットの作成 |
mv | オブジェクトの移動 |
rb | S3バケットの削除 |
rm | オブジェクトの削除 |
sync | ディレクトリの同期 |
website | Webサイトとして公開 |
バケットの操作
■ aws s3 mb <S3Uri>
:バケットの作成
$ aws s3 mb s3://test-trial-20160215 make_bucket: s3://test-trial-20160215/
- バケット名はリージョンでユニークである必要がある
- 「s3://」スキームを使う
■ aws s3 rb <S3Uri>
:バケットの作成
$ aws s3 rb s3://test-trial-20160215 remove_bucket: s3://test-trial-20160215/
- バケット内にオブジェクトが残っている場合は「--force」オプションを使う
aws s3 rb <S3Uri> --force
ファイルのコピー
■ aws s3 cp <LocalPath> <S3Uri>
:ローカル → S3
■ aws s3 cp <S3Uri> <LocalPath>
:S3 → ローカル
■ aws s3 cp <S3Uri> <S3Uri>
:S3 → S3
$ aws s3 cp hoge.txt s3://test-trial-20160215 upload: .\hoge.txt to s3://test-trial-20160215/hoge.txt $ aws s3 cp s3://test-trial-20160215/hoge.txt ./ download: s3://test-trial-20160215/hoge.txt to .\hoge.txt $ aws s3 cp s3://test-trial-20160215/hoge.txt s3://test-trial-20160215/hoge/ copy: s3://test-trial-20160215/hoge.txt to s3://test-trial-20160215/hoge/hoge.txt $ aws s3 cp hogeDir/ s3://test-trial-20160215 --recursive upload: hogeDir\hoge3.txt to s3://test-trial-20160215/hoge3.txt upload: hogeDir\hoge1.txt to s3://test-trial-20160215/hoge1.txt upload: hogeDir\hoge2.txt to s3://test-trial-20160215/hoge2.txt
- スラッシュ(/)で終わっていない場合はファイルとみなされる
- 「--recursive」オプションをつけると再帰的にコピーされる
ファイルの移動
■ aws s3 mv <LocalPath> <S3Uri>
:ローカル → S3
■ aws s3 mv <S3Uri> <LocalPath>
:S3 → ローカル
■ aws s3 mv <S3Uri> <S3Uri>
:S3 → S3
使い方はaws s3 cp
と同じ。
一覧表示
■ aws s3 ls [<S3Uri>]
$ aws s3 ls 2016-02-25 18:17:13 test-trial-20160215 $ aws s3 ls s3://test-trial-20160215 PRE hoge/ 2016-02-25 18:21:07 0 hoge.txt 2016-02-25 18:28:14 0 hoge1.txt 2016-02-25 18:28:14 0 hoge2.txt 2016-02-25 18:28:14 0 hoge3.txt $ aws s3 ls s3://test-trial-20160215 --recursive 2016-02-25 18:21:07 0 hoge.txt 2016-02-25 18:22:54 0 hoge/hoge.txt 2016-02-25 18:28:14 0 hoge1.txt 2016-02-25 18:28:14 0 hoge2.txt 2016-02-25 18:28:14 0 hoge3.txt
- パスが無い場合はバケットの一覧が表示される
- 「--recursive」オプションをつけると再帰的に表示される
ファイルの削除
■ aws s3 rm <S3Uri>
$ aws s3 rm s3://test-trial-20160215/hoge.txt delete: s3://test-trial-20160215/hoge.txt $ aws s3 rm s3://test-trial-20160215/hoge/ --recursive delete: s3://test-trial-20160215/hoge/hoge.txt
- 「--recursive」オプションをつけると再帰的に削除される
ディレクトリ単位の同期
■ aws s3 sync <LocalPath> <S3Uri>
$ aws s3 sync ./ s3://test-trial-20160215 upload: .\hoge.txt to s3://test-trial-20160215/hoge.txt upload: .\aws_s3.txt to s3://test-trial-20160215/aws_s3.txt upload: hogeDir\hoge2.txt to s3://test-trial-20160215/hogeDir/hoge2.txt upload: hogeDir\hoge3.txt to s3://test-trial-20160215/hogeDir/hoge3.txt upload: hogeDir\hoge1.txt to s3://test-trial-20160215/hogeDir/hoge1.txt $ rm hoge*.txt $ aws s3 sync ./ s3://test-trial-20160215 \ --delete \ --exclude ".git*/*" \ --exclude ".gitignore" delete: s3://test-trial-20160215/hoge.txt delete: s3://test-trial-20160215/hoge1.txt delete: s3://test-trial-20160215/hoge2.txt delete: s3://test-trial-20160215/hoge3.txt
- ローカルの状態をS3に反映する
- 削除されたファイルは同期されない
- 「--delete」オプションで削除ファイルも同期される
- 「--exclude」オプションで同期対象外のファイルを指定できる
- 「--include」オプションで同期対象のファイルを指定できる
- 「--exclude」「--include」は複数指定できる(表記方法の詳細はここ)
Webサイトとして公開
■ aws s3 website <S3Uri> [--index-document <value>] [--error-document <value>]
$ aws s3 website s3://test-trial-20160215 \ --index-documen index.html \ --error-document error.html
- 「http://test-trial-20160215.s3-website-ap-northeast-1.amazonaws.com/」でアクセスできるようになる
- リージョンが異なる場合は注意
- 「--index-documen」オプションで初期ページを指定できる
- 「--error-documen」オプションでエラーページを指定できる
- ファイルへのアクセス権を付与しないと公開しても見れない
高レベルコマンド aws s3api
高レベルコマンドはたくさんあるので、一部だけ紹介。
バケットの存在確認
■ aws s3api head-bucket --bucket <bucket_name>
- バケット存在、およびアクセス権限があるか確認するコマンド
アクセス権限を付与
aws s3api put-object-acl [--acl <value>] [--access-control-policy <value>] --bucket <value> [--content-md5 <value>] [--grant-full-control <value>] [--grant-read <value>] [--grant-read-acp <value>] [--grant-write <value>] [--grant-write-acp <value>] --key <value> [--request-payer <value>] [--cli-input-json <value>] [--generate-cli-skeleton]
- 「--key」にはバケット配下のファイルのパスを指定する
- 例)hogeDir/hoge1.txt
- 「--acl」には以下を指定できる。(詳細はここ)
- private
- public-read
- public-read-write
- authenticated-read
- aws-exec-read
- bucket-owner-read
- bucket-owner-full-control
以下は複数ファイルのアクセス権限を変更する例。
■直列に処理する例
$ aws s3 ls --recursive s3://test-trial-20160215/ \ | awk '{print $4}' \ | xargs -I{} \ aws s3api put-object-acl \ --acl public-read \ --bucket test-trial-20160215 \ --key "{}"
■4並列に処理する例(xargsの-Pを使用)
$ aws s3 ls --recursive s3://test-trial-20160215/ \ | awk '{print $4}' \ | xargs -P4 -I{} \ aws s3api put-object-acl \ --acl public-read \ --bucket test-trial-20160215 \ --key "{}"
s3cmdというツールを使えばもっと簡単に書けるが、AWS公式ツールではないようなのでここでは控える。
参考:
例
gitから資材を取得してWebサイトを公開するまでのスクリプトは以下のようになる。
#!/bin/sh -x export git_repository="https://repository/test-trial-20160215.git" export bucket_name="test-trial-20160215" # バケット名 git clone ${git_repository} aws s3 mb s3://${bucket_name} aws s3 sync ./ s3://${bucket_name} \ --delete \ --exclude ".git*/*" \ --exclude ".gitignore" aws s3 ls --recursive s3://${bucket_name}/ \ | awk '{print $4}' \ | xargs -P4 -I{} \ aws s3api put-object-acl \ --acl public-read \ --bucket ${bucket_name} \ --key "{}" aws s3 website s3://${bucket_name} \ --index-documen index.html \ --error-document error.html
「#!/bin/sh -x」でエラーが発生しても無視して処理を続行している。