IT人材をお探しの方は、まずはお電話ください。担当営業がすぐにご要望にマッチした人材をご提案いたします。
tel:03-6409-6766 お急ぎの方はお電話ください

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

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

IT・技術関連 更新日:2024.09.05
エンジニア採用
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エンジニアAWSパートナー/Salesforce認定コンサルティングパートナー 認定企業
ITエンジニア派遣サービス事業を行っています。AWSやSalesforceなど専門領域に特化したITエンジニアが4,715名在籍し、常時100名以上のITエンジニアの即日派遣が可能です。

・2021年:AWS Japan Certification Award 2020 ライジングスター of the Year 受賞
・2022年3月:人材サービス型 AWSパートナー認定
・AWS認定資格保有者数1,154名(2024年6月現在)
・Salesforce認定コンサルティングパートナー
・Salesforce認定資格者276名在籍(2024年5月現在)
・LPIC+CCNA 認定資格者:472 名(2024年6月時点)
ITエンジニアの派遣を利用したい企業様へ
ITエンジニアを派遣で採用したい企業様へ
  • 求人・転職サイトや自社採用サイトを使っているが、自社に合ったITエンジニアが応募してこない…
  • すぐに採用したいが、応募がぜんぜん集まらない
こんな悩みをお持ちの採用・人事担当者の方は、
オープンアップITエンジニアをご検討ください!

当社のITエンジニア派遣サービスは

  • 派遣スピードが速い!(最短即日)
  • 4,500名のエンジニアから貴社にマッチした人材を派遣
  • 正社員雇用も可能

こんな特長があり、貴社の事業やプロジェクトに合った最適なITエンジニアを派遣可能です。
まずは下記ボタンから無料でご相談ください。

無料相談のお申し込みはこちら
IT・技術関連の記事一覧へ

カテゴリから記事を探す

すべての記事一覧へ