演習: S3サービスの操作 (ハイレベルコマンド)

AWS S3ハイレベルコマンドを利用して、以下の操作を実施してみます。

  • S3バケットの作成

  • S3バケットにファイル保存

  • ファイルをS3バケット上の新規フォルダに置く

  • S3バケット上のファイルのダウンロード

  • S3バケット上のファイルの移動

  • S3バケット上のファイルの削除

  • S3バケットとの同期

  • S3バケットにwebsite ホスティング設定

  • S3バケット上のファイルに対する署名付きURLの作成

  • S3バケットを削除

0. 準備

リージョンの決定

作成するS3バケットのリージョンを決めます。

変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

1. S3バケットの作成

1.1. バケット名の決定

S3バケット名を決めます。

注釈

英小文字と数字が使えます。S3上でユニークである必要があります。英大文字は使用できません。

変数の設定:

S3_BUCKET_PREFIX='handson-cli-s3high'

コマンド:

AWS_ID=$( \
  aws sts get-caller-identity \
    --query 'Account' \
    --output text \
) \
  && echo ${AWS_ID}

結果(例):

XXXXXXXXXXXX

変数の設定:

S3_BUCKET_NAME="${S3_BUCKET_PREFIX}-${AWS_ID}" \
  && echo ${S3_BUCKET_NAME}

結果(例):

handson-cli-s3high-XXXXXXXXXXXX

1.2. 同名のS3バケットの不存在確認

コマンド:

! aws s3 ls s3://${S3_BUCKET_NAME}/

結果(例):

A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist

1.3. S3バケット作成 (mb)

S3バケットを作成します。

コマンド:

aws s3 mb s3://${S3_BUCKET_NAME}

結果(例):

make_bucket: s3://handson-cli-s3high-XXXXXXXXXXXX

1.4. S3バケットの確認 (ls)

S3バケットを表示します。

コマンド:

aws s3 ls | grep ${S3_BUCKET_NAME}

結果(例):

2019-09-23 01:23:45 handson-cli-s3high-XXXXXXXXXXXX

1.5. ファイル不存在の確認 (ls)

S3バケットの中身を表示すると、まだファイルがありません。

コマンド:

aws s3 ls s3://${S3_BUCKET_NAME}/

結果(例):

(戻り値なし)

2. S3バケットにファイル保存

2.1. サンプルファイルの準備

変数の設定:

FILE_LOCAL='handson.data'

コマンド:

touch ${FILE_LOCAL}

2.2. ファイルをアップロード (cp)

コマンド:

aws s3 cp ${FILE_LOCAL} s3://${S3_BUCKET_NAME}/

結果(例):

upload: ./handson.data to s3://handson-cli-s3high-XXXXXXXXXXXX/handson.data

2.3. ファイル存在の確認 (ls)

S3バケットの中身を表示すると、転送されたファイルがあることがわかります。

コマンド:

aws s3 ls s3://${S3_BUCKET_NAME}/

結果(例):

2019-09-23 01:23:45        0 handson.data

3. ファイルをS3バケット上の新規フォルダに置く場合 (cp)

3.1. フォルダ名の決定

変数の設定:

S3_PATH='dir1'

3.2. ファイルをアップロード (cp)

コマンド:

aws s3 cp ${FILE_LOCAL} s3://${S3_BUCKET_NAME}/${S3_PATH}/

結果(例):

upload: ./handson.data to s3://handson-cli-s3high-XXXXXXXXXXXX/dir1/handson.data

4. S3バケット上のファイルのダウンロード (cp)

変数の設定:

S3_OBJ='handson.data'
FILE_LOCAL='local.data'

コマンド:

aws s3 cp s3://${S3_BUCKET_NAME}/${S3_OBJ} ${FILE_LOCAL}

結果(例):

download: s3://handson-cli-s3high-XXXXXXXXXXXX/handson.data to ./local.data

確認

コマンド:

ls ${FILE_LOCAL}

結果(例):

local.data

5. S3バケット上のファイルの移動 (mv)

変数の設定:

S3_BUCKET_NAME_SRC=${S3_BUCKET_NAME}
S3_BUCKET_NAME_DST=${S3_BUCKET_NAME}
S3_OBJ_SRC=dir1/handson.data
S3_OBJ_DST=dir2/handson2.data

コマンド:

aws s3 mv s3://${S3_BUCKET_NAME_SRC}/${S3_OBJ_SRC} \
  s3://${S3_BUCKET_NAME_DST}/${S3_OBJ_DST}

結果(例):

move: s3://handson-cli-s3high-XXXXXXXXXXXX/dir1/handson.data to s3://handson-cli-s3high-XXXXXXXXXXXX/dir2/handson2.data

コマンド:

! aws s3 ls s3://${S3_BUCKET_NAME_SRC}/${S3_OBJ_SRC}

結果(例):

(戻り値なし)

コマンド:

aws s3 ls s3://${S3_BUCKET_NAME_DST}/${S3_OBJ_DST}

結果(例):

2019-09-23 01:23:45          0 handson2.data

6. S3バケット上のファイルの削除 (rm)

変数の設定:

S3_OBJ=${S3_OBJ_DST}

コマンド:

aws s3 ls s3://${S3_BUCKET_NAME}/${S3_OBJ}

結果(例):

2019-09-23 01:23:45          0 handson2.data

コマンド:

aws s3 rm s3://${S3_BUCKET_NAME}/${S3_OBJ}

結果(例):

delete: s3://handson-cli-s3high-XXXXXXXXXXXX/dir2/handson2.data

7. S3バケットとの同期 (sync)

7.1. コンテンツの用意

コマンド:

git clone https://github.com/opelab/handson-cli-web-contents.git \
  && cd handson-cli-web-contents/

結果(例):

Cloning into 'handson-cli-web-contents'...
remote: Counting objects: 5, done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 5
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.

コマンド:

ls

結果(例):

error.html  img.jpg         index.html

7.2. コンテンツの同期

クライアントPCに保存してあるサンプルコンテンツを、先程作成したコンテンツ公開用のS3バケットに同期します。

コマンド:

aws s3 sync . "s3://${S3_BUCKET_NAME}/" \
   --acl public-read \
   --exclude ".git*"

結果(例):

upload: ./index.html to s3://handson-cli-s3high-XXXXXXXXXXXX/index.html
upload: ./img.jpg to s3://handson-cli-s3high-XXXXXXXXXXXX/img.jpg
upload: ./error.html to s3://handson-cli-s3high-XXXXXXXXXXXX/error.html

7.3. 同期の確認 (ls)

S3バケットの中身を表示すると、同期されたファイルがあることがわかります。

コマンド:

aws s3 ls s3://${S3_BUCKET_NAME}/

結果(例):

2019-09-23 01:23:45    186 error.html
2019-09-23 01:23:45      0 handson.data
2019-09-23 01:23:45 184911 img.jpg
2019-09-23 01:23:45    384 index.html

8. S3バケットにwebsite ホスティング設定 (website)

8.1. website ホスティングの設定

コマンド:

aws s3 website "s3://${S3_BUCKET_NAME}" \
  --index-document index.html \
  --error-document error.html

結果(例):

(戻り値なし)

8.2. ブラウザでコンテンツにアクセス

コマンド:

S3_BUCKET_ENDPOINT="${S3_BUCKET_NAME}.s3-website-$(aws s3api get-bucket-location --bucket ${S3_BUCKET_NAME} --output text).amazonaws.com" \
    && echo ${S3_BUCKET_ENDPOINT}

結果(例):

handson-cli-s3high-XXXXXXXXXXXX.s3-website-ap-northeast-1.amazonaws.com

WebブラウザでEndPointにアクセスしてコンテンツが表示されればOKです。

9. S3バケット上のファイルに対する署名付きURLの作成

変数の設定:

FILE_NAME='img.jpg'

変数の設定:

S3_PRESIGN_SECONDS='100'

コマンド:

aws s3 presign s3://${S3_BUCKET_NAME}/${FILE_NAME} \
  --expires-in ${S3_PRESIGN_SECONDS}

結果(例):

https://handson-cli-s3high-XXXXXXXXXXXX.s3.amazonaws.com/img.jpg?AWSAccessKeyId=ASIAxxxxxxxxxxxxxxxx&Expires=1234567890&x-amz-security-token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxx%3D

10. S3バケットを削除 (rb)

10.1. S3バケットを空にする

事前にファイルを全て削除しておく必要があります。

コマンド:

aws s3 rm s3://${S3_BUCKET_NAME}/ \
  --recursive

結果(例):

delete: s3://handson-cli-s3high-XXXXXXXXXXXX/error.html
delete: s3://handson-cli-s3high-XXXXXXXXXXXX/index.html
delete: s3://handson-cli-s3high-XXXXXXXXXXXX/img.jpg
delete: s3://handson-cli-s3high-XXXXXXXXXXXX/handson.data

10.2. S3バケットの削除

コマンド:

aws s3 rb s3://${S3_BUCKET_NAME}

結果(例):

remove_bucket: s3://handson-cli-s3high-XXXXXXXXXXXX/

コマンド:

! aws s3 ls s3://${S3_BUCKET_NAME}/

結果(例):

A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist

完了