AWS CLI syncコマンドでローカルフォルダとの同期方法

環境
AWS CLI
AWS S3

操作方法
1.sync同期実行前

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ aws s3 ls s3://arkgame-bucket/ --recursive
2022-04-01 08:32:45 0 sync/
2022-04-01 08:38:39 0 sync/xxxxxxxxxx.txt
2022-04-01 08:13:29 0 test1.csv
$ aws s3 ls s3://arkgame-bucket/ --recursive 2022-04-01 08:32:45 0 sync/ 2022-04-01 08:38:39 0 sync/xxxxxxxxxx.txt 2022-04-01 08:13:29 0 test1.csv
$ aws s3 ls s3://arkgame-bucket/ --recursive
2022-04-01 08:32:45 0 sync/
2022-04-01 08:38:39 0 sync/xxxxxxxxxx.txt
2022-04-01 08:13:29 0 test1.csv

ディレクトリを表示します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ tree
.
├── dir1
│ ├── dir1_1
│ │ ├── test1.csv
│ │ ├── test1.txt
│ │ ├── test2.txt
│ │ └── test3.txt
│ ├── dir1_2
│ │ └── test2.csv
│ └── dir1_3
├── test1.md
└── test4.txt
4 directories, 7 files
$ tree . ├── dir1 │ ├── dir1_1 │ │ ├── test1.csv │ │ ├── test1.txt │ │ ├── test2.txt │ │ └── test3.txt │ ├── dir1_2 │ │ └── test2.csv │ └── dir1_3 ├── test1.md └── test4.txt 4 directories, 7 files
$ tree
.
├── dir1
│   ├── dir1_1
│   │   ├── test1.csv
│   │   ├── test1.txt
│   │   ├── test2.txt
│   │   └── test3.txt
│   ├── dir1_2
│   │   └── test2.csv
│   └── dir1_3
├── test1.md
└── test4.txt

4 directories, 7 files

2.sync同期を実行します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ aws s3 sync . s3://arkgame-bucket/sync
upload: dir1/dir1_1/test1.csv to s3://arkgame-bucket/sync/dir1/dir1_1/test1.csv
upload: dir1/dir1_1/test3.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test3.txt
upload: ./test1.md to s3://arkgame-bucket/sync/test1.md
upload: dir1/dir1_1/test2.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test2.txt
upload: dir1/dir1_1/test1.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test1.txt
upload: ./test4.txt to s3://arkgame-bucket/sync/test4.txt
upload: dir1/dir1_2/test2.csv to s3://arkgame-bucket/sync/dir1/dir1_2/test2.csv
$ aws s3 sync . s3://arkgame-bucket/sync upload: dir1/dir1_1/test1.csv to s3://arkgame-bucket/sync/dir1/dir1_1/test1.csv upload: dir1/dir1_1/test3.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test3.txt upload: ./test1.md to s3://arkgame-bucket/sync/test1.md upload: dir1/dir1_1/test2.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test2.txt upload: dir1/dir1_1/test1.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test1.txt upload: ./test4.txt to s3://arkgame-bucket/sync/test4.txt upload: dir1/dir1_2/test2.csv to s3://arkgame-bucket/sync/dir1/dir1_2/test2.csv
$ aws s3 sync . s3://arkgame-bucket/sync
upload: dir1/dir1_1/test1.csv to s3://arkgame-bucket/sync/dir1/dir1_1/test1.csv
upload: dir1/dir1_1/test3.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test3.txt
upload: ./test1.md to s3://arkgame-bucket/sync/test1.md
upload: dir1/dir1_1/test2.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test2.txt
upload: dir1/dir1_1/test1.txt to s3://arkgame-bucket/sync/dir1/dir1_1/test1.txt
upload: ./test4.txt to s3://arkgame-bucket/sync/test4.txt
upload: dir1/dir1_2/test2.csv to s3://arkgame-bucket/sync/dir1/dir1_2/test2.csv

3.sync同期実行後確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ tree
.
├── dir1
│ ├── dir1_1
│ │ ├── test1.csv
│ │ ├── test1.txt
│ │ ├── test2.txt
│ │ └── test3.txt
│ ├── dir1_2
│ │ └── test2.csv
│ └── dir1_3
├── test1.md
└── test4.txt
4 directories, 7 files
$ tree . ├── dir1 │ ├── dir1_1 │ │ ├── test1.csv │ │ ├── test1.txt │ │ ├── test2.txt │ │ └── test3.txt │ ├── dir1_2 │ │ └── test2.csv │ └── dir1_3 ├── test1.md └── test4.txt 4 directories, 7 files
$ tree
.
├── dir1
│   ├── dir1_1
│   │   ├── test1.csv
│   │   ├── test1.txt
│   │   ├── test2.txt
│   │   └── test3.txt
│   ├── dir1_2
│   │   └── test2.csv
│   └── dir1_3
├── test1.md
└── test4.txt

4 directories, 7 files

確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ aws s3 ls s3://arkgame-bucket/ --recursive
2022-04-01 08:32:45 0 sync/
2022-04-01 08:40:47 0 sync/dir1/dir1_1/test1.csv
2022-04-01 08:40:47 0 sync/dir1/dir1_1/test1.txt
2022-04-01 08:40:47 0 sync/dir1/dir1_1/test2.txt
2022-04-01 08:40:47 0 sync/dir1/dir1_1/test3.txt
2022-04-01 08:40:47 0 sync/dir1/dir1_2/test2.csv
2022-04-01 08:40:47 0 sync/test1.md
2022-04-01 08:40:47 0 sync/test4.txt
2022-04-01 08:38:39 0 sync/xxxxxxxxxx.txt
2022-04-01 08:13:29 0 test1.csv
$ aws s3 ls s3://arkgame-bucket/ --recursive 2022-04-01 08:32:45 0 sync/ 2022-04-01 08:40:47 0 sync/dir1/dir1_1/test1.csv 2022-04-01 08:40:47 0 sync/dir1/dir1_1/test1.txt 2022-04-01 08:40:47 0 sync/dir1/dir1_1/test2.txt 2022-04-01 08:40:47 0 sync/dir1/dir1_1/test3.txt 2022-04-01 08:40:47 0 sync/dir1/dir1_2/test2.csv 2022-04-01 08:40:47 0 sync/test1.md 2022-04-01 08:40:47 0 sync/test4.txt 2022-04-01 08:38:39 0 sync/xxxxxxxxxx.txt 2022-04-01 08:13:29 0 test1.csv
$ aws s3 ls s3://arkgame-bucket/ --recursive
2022-04-01 08:32:45          0 sync/
2022-04-01 08:40:47          0 sync/dir1/dir1_1/test1.csv
2022-04-01 08:40:47          0 sync/dir1/dir1_1/test1.txt
2022-04-01 08:40:47          0 sync/dir1/dir1_1/test2.txt
2022-04-01 08:40:47          0 sync/dir1/dir1_1/test3.txt
2022-04-01 08:40:47          0 sync/dir1/dir1_2/test2.csv
2022-04-01 08:40:47          0 sync/test1.md
2022-04-01 08:40:47          0 sync/test4.txt
2022-04-01 08:38:39          0 sync/xxxxxxxxxx.txt
2022-04-01 08:13:29          0 test1.csv

ローカルに存在して、S3に存在しないファイルがアップロードされました。

3.追加・更新操作を行います(S3の内容をローカルと同期)
S3に存在して、ローカルに存在しないファイルがダウンロードされました。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ aws s3 sync s3://arkgame-bucket/sync .
download: s3://arkgame-bucket/sync/xxxxxxxxxx.txt to ./xxxxxxxxxx.txt
$ aws s3 sync s3://arkgame-bucket/sync . download: s3://arkgame-bucket/sync/xxxxxxxxxx.txt to ./xxxxxxxxxx.txt
$ aws s3 sync s3://arkgame-bucket/sync .
download: s3://arkgame-bucket/sync/xxxxxxxxxx.txt to ./xxxxxxxxxx.txt

4.ファイルの削除

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ rm test1.md
$
$ aws s3 sync . s3://arkgame-bucket/sync
$
$ aws s3 sync . s3://arkgame-bucket/sync --delete
delete: s3://arkgame-bucket/sync/test1.md
$ rm test1.md $ $ aws s3 sync . s3://arkgame-bucket/sync $ $ aws s3 sync . s3://arkgame-bucket/sync --delete delete: s3://arkgame-bucket/sync/test1.md
$ rm test1.md 
$ 
$ aws s3 sync . s3://arkgame-bucket/sync
$ 
$ aws s3 sync . s3://arkgame-bucket/sync --delete
delete: s3://arkgame-bucket/sync/test1.md

 

AWS

Posted by arkgame