LoginSignup
1
0

More than 1 year has passed since last update.

【AWS CLI】S3基本操作

Posted at

まえがき

CLI経由でのS3操作を本稿にまとめていく。
高レベルコマンド(aws s3)とAPIレベルコマンド(aws s3api)の2種類あるので、それぞれで出来ることをまとめていく。

高レベルコマンド(aws s3)

できること
・バケット一覧表示
・バケット作成
・バケット削除
・オブジェクトのコピー
・オブジェクトの移動
・オブジェクトの削除

バケット作成/削除

コマンド

aws s3 mb s3://bucket_name
aws s3 rb s3://bucket_name                  ※オブジェクトが空の場合
aws s3 rb s3://bucket_name --force  ※オブジェクトが空でない場合

実行例

% aws s3 mb s3://20220514hogebucket
make_bucket: 20220514hogebucket

% aws s3 rb s3://20220514bucket
remove_bucket: 20220514bucket

バケット一覧表示

コマンド

aws s3 ls

実行例

% aws s3 ls             
2022-05-14 14:51:03 20220514bucket
2022-05-14 15:01:55 20220514hogebucket

オブジェクト一覧表示

コマンド

// バケット直下を表示
aws s3 ls <bucket_name>
// バケット内の特定フォルダ内を表示
aws s3 ls <bucket_name>/<folder_name>/

実行例

% aws s3 ls s3://20220514bucket
2021-06-15 21:01:44    1035581 java_001.jpg
2021-06-15 21:01:43     669753 java_002.jpg
2021-06-15 21:01:43     391073 java_003.jpg
                                                       PRE sampleDirectory/

% aws s3 ls s3://20220514bucket/sampleDirectory/
2021-06-15 21:01:43     360990 java_004.jpg
2021-06-15 21:01:44     521081 java_005.jpg
2021-06-15 21:01:44     363473 java_006.jpg

オブジェクトのコピー

コマンド

aws s3 cp <source> <target> <options>

1. ローカルファイル → sample_bucket

aws s3 cp ./hello.txt s3://sample_bucket

2. ローカルディレクトリ内の全てファイル → s3://sample_bucket

aws s3 cp ./sampleDirectory s3://sample_bucket

3. バケットA内の全オブジェクト → バケットBへコピー

aws s3 cp s3://bucket-A s3://bucket-B/

4. ストリーム → バケット

echo "hello world" | aws s3 cp - s3://bucket-name/filename.txt

5. ファイル → 標準出力

aws s3 cp s3://bucket-name/filename.txt -

オブジェクトの移動

aws s3 cpの移動バージョンなので割愛。

オブジェクトの削除

コマンド

aws s3 rm <target> <options>

1. オブジェクト削除

aws s3 rm s3://bucket-name/example/filename.txt

2. ディレクトリとディレクトリ内の全オブジェクト削除

aws s3 rm s3://bucket-name/sampleDirectory --recursive

オブジェクトの同期

コマンド

aws s3 aync <source> <target>

1. バケットarticle-picture内にある全オブジェクトを、ローカルディレクトリにコピーする

aws s3 sync s3://picture ./sampleDirectory

実行例

% aws s3 sync s3://article-picture ./sampleDirectory 
download: s3://article-picture/bbb.txt to sampleDirectory/bbb.txt
download: s3://article-picture/xxx.txt to sampleDirectory/xxx.txt
download: s3://article-picture/aaa.txt to sampleDirectory/aaa.txt
download: s3://article-picture/hhh.txt to sampleDirectory/hhh.txt

その他便利なオプション

acl

s3 syncs3 cpで利用できるオプション
・コピーする時にファイルのアクセス権限を設定できる
・3つの権限を設定可能(private/public-read/public-read-write)

aws s3 sync . s3://my-bucket/path --acl public-read

include/exclude

s3 cps3 mvs3 syncs3 rmで利用できるオプション
・対象オブジェクトのフィルタリングをする。

// 拡張子「jpeg」「jpg」のみをコピーする。
$ aws s3 cp . s3://my-bucket/path --include "*.jpg" --include *.jpeg*

APIレベルコマンド(aws s3api)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0