AWS CLIのs3 cp
とs3 sync
の違いをまとめます。
はじめに
こんにちは、@bioerrorlogです。
あるS3バケットのオブジェクトを別のS3バケットにコピーしようと思ったとき、2つのコマンドが頭に浮かびました。
s3 cp
とs3 sync
です。
雰囲気で分かったつもりになっていましたが、自分はまだ両者の違いを明確に説明できない気がしました。
今回は、この2つのコマンドの違いを調べ、整理していきます。
cpとsyncの違い
先に一言で両者の最も大きな違いを言うならば、
cp
はファイルをコピーするコマンド
sync
はディレクトリの更新差分をコピーする(同期する)コマンド
と言えるでしょう。
Descriptionを比較する
ではまず、両者のCLIドキュメントのDescriptionを比較します。
cp — AWS CLI 2.7.22 Command Reference
Description:
Copies a local file or S3 object to another location locally or in S3.
sync — AWS CLI 2.7.22 Command Reference
Description:
Syncs directories and S3 prefixes. Recursively copies new and updated files from the source directory to the destination. Only creates folders in the destination if they contain one or more files.
cp
は単純に"オブジェクトのコピー"をするコマンドであるのに対し、sync
では"新規作成または更新されたファイル"について、"指定ディレクトリ以下を再帰的に"コピーする、ということが分かります ("ひとつ以上のファイルが含まれるフォルダのみを作成する"という挙動特性もあるようです)。
これらcp
とsync
の説明の違いは、ファイルを"コピー"するのか、ディレクトリを"同期(更新差分をコピー)"するのか、という違いとして直感的にも頷けるものでした。
コマンドオプションを比較する
次は、コマンドの実際の使い方を比較します。
具体的には、コマンドに用意されているオプションの差異を調べてみます。
cp
とsync
のコマンドオプションは大半が同じものですが、いくつか異なるものがあります。
まず、sync
コマンドに特有のオプションとしては、以下のものがあります。
- [--size-only]
- [--exact-timestamps]
- [--delete]
[--size-only]
オプションは、sync元のファイルが更新されたか否か(=データコピーを実行するか否か)、をファイルサイズのみで判定することを指定するオプションのようです。
Makes the size of each key the only criteria used to decide whether to sync from source to destination.
[--exact-timestamps]
オプションは、sync元のファイルが更新されたか否かを、タイムスタンプの一致まで見るオプションのようです。
When syncing from S3 to local, same-sized items will be ignored only when the timestamps match exactly. The default behavior is to ignore same-sized items unless the local version is newer than the S3 version.
[--delete]
オプションは、sync先にはあるけれどsync元にはないファイルを削除するオプションです。
Files that exist in the destination but not in the source are deleted during sync.
上記3つのsync
特有のオプションはいずれも、ディレクトリの更新差分をコピーする、というsync
コマンドの機能に深く関与するものと言えるでしょう。
次は、cp
コマンド特有のオプションを見ていきます。
- [--expected-size
] - [--recursive]
[--expected-size <value>]
は、S3へのアップロードサイズが50GBを超える場合に、マルチパートアップロードをエラーなく実行するために必要なオプションのようです。
This argument specifies the expected size of a stream in terms of bytes. Note that this argument is needed only when a stream is being uploaded to s3 and the size is larger than 50GB. Failure to include this argument under these conditions may result in a failed upload due to too many parts in upload.
[--recursive]
は、指定したディレクトリ下の全てのファイルを再帰的にコピーするオプションです。
Command is performed on all files or objects under the specified directory or prefix.
cp
コマンドはsync
オプションと違い、ファイルのコピーをデフォルト挙動としているため、ディレクトリ下を再帰的にコピーしたい場合は[--recursive]
オプションを使うことになります。
これはcp
とsync
の挙動の違いを表す、象徴的なオプションと言えるでしょう。
コマンドの挙動を比較する
最後に、実際にcp
コマンドとsync
コマンドを叩いて挙動を確認することで、両者の違いを実感したいと思います。
まずは単純に、S3バケット間の複数ファイルコピーを行います。
sync
でこれを行うには、以下のシンプルなコマンドを発行します。
$ aws s3 sync s3://cp-sync-source s3://sync-destination copy: s3://cp-sync-source/aaa.txt to s3://sync-destination/aaa.txt copy: s3://cp-sync-source/bbb.txt to s3://sync-destination/bbb.txt
一方、cp
でディレクトリ単位のコピーを行うには、前述の通り--recursive
オプションを付与する必要があります。
$ aws s3 cp s3://cp-sync-source s3://cp-destination --recursive copy: s3://cp-sync-source/bbb.txt to s3://cp-destination/bbb.txt copy: s3://cp-sync-source/aaa.txt to s3://cp-destination/aaa.txt
では次に、ディレクトリ更新差分に対する両者の挙動の違いを見ていきます。
コピー元のS3バケットcp-sync-source
のうち、bbb.txt
を更新し、ccc.txt
ファイルを新規追加しました。
この状態で再びsync
を行うと、
$ aws s3 sync s3://cp-sync-source s3://sync-destination copy: s3://cp-sync-source/bbb.txt to s3://sync-destination/bbb.txt copy: s3://cp-sync-source/ccc.txt to s3://sync-destination/ccc.txt
コピーが実行されるのはbbb.txt
とccc.txt
のみで、aaa.txt
はコピーされません。
一方cp
を実行すると、
$ aws s3 cp s3://cp-sync-source s3://cp-destination --recursive copy: s3://cp-sync-source/bbb.txt to s3://cp-destination/bbb.txt copy: s3://cp-sync-source/aaa.txt to s3://cp-destination/aaa.txt copy: s3://cp-sync-source/ccc.txt to s3://cp-destination/ccc.txt
ディレクトリの更新差分とは関係なく、全ファイルが再びコピーされます。
このように、
cp
はファイルをコピーするコマンド
sync
はディレクトリの更新差分をコピーする(同期する)コマンド
という違いが見て取れました。
おわりに
今回は、AWS CLIコマンドのcp
とsync
の違いを調べました。
大まかな挙動の違いは、文字通り"コピー"なのか"同期"なのか、というのと直感的に近しいものでした。
が、各オプションを比較してみると、同じものと違うものとでそれぞれのコマンドの想定用法が見えてきて勉強になりました。
分かった気になっていても、今一度調べてみると多くの発見があって面白いものです。
[関連記事]
参考
cp — AWS CLI 2.7.22 Command Reference
sync — AWS CLI 2.7.22 Command Reference