AWSアカウント間のS3 syncを実行してみました。
ベルリンの半瀬です。
はじめに
題名の通りですが、アカウントを跨いだS3転送についてEc2からコマンドラインでの実行確認を行いましたので、そのメモを残します。
以下の過去記事とAWSドキュメントを元にしました。
- How do I transfer ownership of Amazon S3 objects to a different AWS account? | AWS
- クロスアカウントで S3 sync するための権限設定 | Developers.IO
やってみます
以下のような形で実行します。EC2は送り先で起動。
簡単です。と言いつつもポリシーの記述間違いでハマったことは内緒です。
- 送信元(Source)の情報
- AWS Account ID : 111111111111
- S3 Bucket : test-s3-sync-src (Frankfurt)
- 送信先(Destination)の情報
- AWS Account ID : 222222222222
- S3 Bucket : test-s3-sync-dst (Frankfurt)
とします。
送信元バケットでバケットポリシー付与
送信元アカウント(ID:111111111111)のバケット「test-s3-sync-src」に、以下のポリシーを付与します。
Principalで送信先アカウントID(2222222222222)を指定し、送信先からのアクセスを許可しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { "Version" : "2012-10-17" , "Statement" : [ { "Sid" : "DelegateS3Access" , "Effect" : "Allow" , "Principal" :{ "AWS" : "222222222222" }, "Action" : "s3:*" , "Resource" : [ "arn:aws:s3:::test-s3-sync-src/*" , "arn:aws:s3:::test-s3-sync-src" ] } ] } |
送信先アカウントでCLI操作用のIAM Roleを準備
作業場所はEC2なので、IAM Ec2 Roleを準備します。
送信元ではListBucketとGetObject、送信先ではListBucketとPutObjectをポリシーで指定します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | { "Version" : "2012-10-17" , "Statement" : [ { "Effect" : "Allow" , "Action" : [ "s3:ListBucket" , "s3:GetObject" ], "Resource" : [ "arn:aws:s3:::test-s3-sync-src" , "arn:aws:s3:::test-s3-sync-src/*" ] }, { "Effect" : "Allow" , "Action" : [ "s3:ListBucket" , "s3:PutObject" , "s3:PutObjectAcl" ], "Resource" : [ "arn:aws:s3:::test-s3-sync-dst" , "arn:aws:s3:::test-s3-sync-dst/*" ] } ] } |
作業用Ec2を起動
します。前項で設定したIAM roleの付与忘れず。
起動後にログインし、今回の実行環境を確認します。
1 2 | $ aws --version aws-cli /1 .14.9 Python /2 .7.12 Linux /4 .9.76-3.78.amzn1.x86_64 botocore /1 .8.13 |
CLIバージョンは上の通りです。
S3 syncを実行
IAM Role が効いていることを確認します。
送信先アカウントの「test-s3-sync-dst」が見えていることを確認。
1 2 | $ aws s3 ls s3: //test-s3-sync-dst/ 2018-01-22 18:57:37 288381 test .jpg |
テストで配置した画像ファイルが確認できます。
送信元アカウントの「test-s3-sync-src」が見えていることを確認。
1 2 | $ aws s3 ls s3: //test-s3-sync-src/ 2018-01-22 18:51:16 1422172380 test .gz |
test.gz(約1.3Gbyte)を確認に使用します。
実行。
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ aws s3 sync s3: //test-s3-sync-src s3: //test-s3-sync-dst Completed 120.0 MiB /1 .3 GiB (163.1 MiB /s ) with 1 file (s) remaining Completed 208.0 MiB /1 .3 GiB (173.3 MiB /s ) with 1 file (s) remaining Completed 320.0 MiB /1 .3 GiB (198.7 MiB /s ) with 1 file (s) remaining Completed 424.0 MiB /1 .3 GiB (196.9 MiB /s ) with 1 file (s) remaining Completed 576.0 MiB /1 .3 GiB (209.9 MiB /s ) with 1 file (s) remaining Completed 712.0 MiB /1 .3 GiB (213.8 MiB /s ) with 1 file (s) remaining Completed 856.0 MiB /1 .3 GiB (220.5 MiB /s ) with 1 file (s) remaining Completed 984.0 MiB /1 .3 GiB (224.6 MiB /s ) with 1 file (s) remaining Completed 1.1 GiB /1 .3 GiB (227.3 MiB /s ) with 1 file (s) remaining Completed 1.2 GiB /1 .3 GiB (229.3 MiB /s ) with 1 file (s) remaining Completed 1.3 GiB /1 .3 GiB (227.9 MiB /s ) with 1 file (s) remaining copy: s3: //test-s3-sync-src/test .gz to s3: //test-s3-sync-dst/test .gz |
実行が完了しました。
実行時はだいたい200M/secほど出ています。1G程度だったので、あっという間に転送されました。
ファイルが転送されていることを確認。
1 2 3 | $ aws s3 ls s3: //test-s3-sync-dst/ 2018-01-22 18:57:37 288381 test .jpg 2018-01-22 20:14:15 1422172380 test .gz |
以上です。
おわりに
S3ファイルのアカウント間転送について実行確認を行ない、具体的な実行コマンドサンプルについてご紹介しました。なお、実行したコマンドは同じリージョン間での転送ですので、異なるリージョンのバケットを指定する場合は--source-region
、--region
の指定が必要となります。
その他のご参考
それではー。