2021/08/13

AWS S3 syncとは?AWS S3 syncの設定方法・同期したファイルへのアクセス・オブジェクトのフィルタリング

 
  

AWS S3 syncとは?


今回は、AWS S3 syncについて説明します。AWS S3 syncを設置すると、オブジェクトを同期することができます。AWS S3 syncに興味のある方はぜひご覧ください。

AWS S3 syncの設定方法

基本的な使い方として、AWS S3 syncを付けてみます。実際のソースコードを見てみましょう。

$ aws s3 sync . s3://my-bucket/path
upload: MySubdirectory\MyFile3.txt to s3://my-bucket/path/MySubdirectory/MyFile3.txt
upload: MyFile2.txt to s3://my-bucket/path/MyFile2.txt
upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt

これで、my-bucketのpathをディレクトリに同期できます。

同期したファイルへのアクセス

AWS S3 syncでは、同期したファイルへのアクセスができます。実際のソースコードを見てみましょう。

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

AWS S3 syncにaclを追加し、その後にprivate、public-read、public-writeのいずれかを設置する必要があります。

オブジェクトのみを除外してフィルタリング

AWS S3 syncでは、オブジェクトのみを除外してフィルタリングできます。実際のソースコードを見てみましょう。

Local directory contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt

// Exclude all .txt files, resulting in only MyFile2.rtf being copied
$ aws s3 cp . s3://my-bucket/path --exclude "*.txt"

// Exclude all .txt files but include all files with the "MyFile*.txt" format, resulting in, MyFile1.txt, MyFile2.rtf, MyFile88.txt being copied
$ aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt"

// Exclude all .txt files, but include all files with the "MyFile*.txt" format, but exclude all files with the "MyFile?.txt" format resulting in, MyFile2.rtf and MyFile88.txt being copied
$ aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt" --exclude "MyFile?.txt"

AWS S3 syncに–excludeを追加する必要があります。オプションは指定した順番にフィルタリングされます。

指定したオブジェクトのみをフィルタリング

AWS S3 syncでは、指定したオブジェクトのみをフィルタリングできます。実際のソースコードを見てみましょう。

Local directory contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt

// Include all .txt files, resulting in MyFile1.txt and MyFile88.txt being copied
$ aws s3 cp . s3://my-bucket/path --include "*.txt"

// Include all .txt files but exclude all files with the "MyFile*.txt" format, resulting in no files being copied
$ aws s3 cp . s3://my-bucket/path --include "*.txt" --exclude "MyFile*.txt"

// Include all .txt files, but exclude all files with the "MyFile*.txt" format, but include all files with the "MyFile?.txt" format resulting in MyFile1.txt being copied

$ aws s3 cp . s3://my-bucket/path --include "*.txt" --exclude "MyFile*.txt" --include "MyFile?.txt"

AWS S3 syncに–includeを追加する必要があります。オプションは指定した順番にフィルタリングされます。

同期中に指定したファイルを削除

AWS S3 syncでは、同期中に指定したファイルを削除できます。実際のソースコードを見てみましょう。

Assume local directory and s3://my-bucket/path currently in sync and each contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt
'''

// Sync with delete, excluding files that match a pattern. MyFile88.txt is deleted, while remote MyFile1.txt is not.
$ aws s3 sync . s3://my-bucket/path --delete --exclude "path/MyFile?.txt"
delete: s3://my-bucket/path/MyFile88.txt
'''

// Sync with delete, excluding MyFile2.rtf - local file is NOT deleted
$ aws s3 sync s3://my-bucket/path . --delete --exclude "./MyFile2.rtf"
download: s3://my-bucket/path/MyFile1.txt to MyFile1.txt
'''

// Sync with delete, local copy of MyFile2.rtf is deleted
$ aws s3 sync s3://my-bucket/path . --delete
delete: MyFile2.rtf

AWS S3 syncに–deleteを追加し、その後に–excludeか–includeを設置することで、削除するファイルを決定できます。

指定したユーザーやグループにアクセスを許可

AWS S3 syncでは、指定したユーザーやグループにアクセスを許可できる実際のソースコードを見てみましょう。

--grants Permission=Grantee_Type=Grantee_ID
         [Permission=Grantee_Type=Grantee_ID ...]

Permissionにはread、readacl、writeacl、fullのいずれかを設置し、許可するものを決められます。Grantee_Typeにはuri、emailaddress、idのいずれかを設置し、誰に許可しているかを判断させます。

同じサイズのファイルを同期

AWS S3 syncでは、同じサイズのファイルを同期できます。実際のソースコードを見てみましょう。

S3内のファイル

$ aws s3 ls s3://test.takahashi.yusuke/ 2015-06-26 18:56:05 4847 document.txt

ローカル内のファイル

$ ll ~/docs/ total 16 drwxr-xr-x 3 takahashiyusuke staff 102 6 25 13:17 . drwxr-xr-x+ 60 takahashiyusuke staff 2040 6 25 13:16 .. -rw-r--r-- 1 takahashiyusuke staff 4847 6 26 18:54 document.txt

–exact-timestmpsをつけて同期

$ aws s3 sync --exact-timestamps s3://test.takahashi.yusuke ~/docs download: s3://test.takahashi.yusuke/document.txt to ./document.txt

ファイルサイズが同じであるときはファイルの同期ができませんが、–exact-timestmpsを追加することで対処できます。

まとめ


いかがだったでしょうか。AWS S3 syncのソースコードを使うことで、オブジェクトの同期をすることができます。

他にも同期したファイルにアクセスしたり、オブジェクトをフィルタリングしたり、同期中に指定したファイルを削除することもできます。ぜひご自身でもAWS S3 syncのソースコードを書いて、理解を深めてください。

ITエンジニアへのキャリアチェンジならキャリアチェンジアカデミー

この記事の監修者・著者

株式会社オープンアップITエンジニア
株式会社オープンアップITエンジニア
未経験からITエンジニアへのキャリアチェンジを支援するサイト「キャリアチェンジアカデミー」を運営。これまで4500人以上のITエンジニアを未経験から育成・排出してきました。
・AWS、salesforce、LPICの合計認定資格取得件数:2100以上(2023年6月時点)
・AWS Japan Certification Award 2020 ライジングスター of the Year 受賞

おすすめの動画

  • 【未経験からIT業界へ転職するなら】相談窓口とスキルの獲得はここで解決!IT転職が一気に有利に!【キャリアチェンジアカデミー】

  • 【費用一切不要】未経験からIT業界へ転職するならまずはここへ相談!【キャリアチェンジアカデミー】

  • 【何のエンジニアになれるのか?】未経験からITエンジニアを目指すとこんな道がある【キャリアチェンジアカデミー】