Open3

AWS CLIによるS3の操作

AWS CLIのインストール

  • 環境
    • macOS Big Sur 11.5.2
    • MacBook Air (M1, 2020)

homebrew

AWS CLI

brew install awscli

% brew info awscli
awscli: stable 2.2.31 (bottled), HEAD
Official Amazon AWS command-line interface
https://aws.amazon.com/cli/
/opt/homebrew/Cellar/awscli/2.2.31 (11,984 files, 87.8MB) *
  Poured from bottle on 2021-08-24 at 12:56:33
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/awscli.rb
License: Apache-2.0

AWS CLIの設定

ログイン用のIAMユーザー→作業対象アカウントにswitch roleして作業、というフローなので以下の手順で ~/.aws/credntials~/.aws/config を設定する。

AWS CLIがAssumeRoleによる自動クレデンシャル取得とMFAに対応しました! | DevelopersIO

% cat .aws/credentials
[aws-iam-user]
aws_access_key_id = AKIAXXXXXXXXXXXXXXXX
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
% cat .aws/config
[profile aws-iam-user]
region = ap-northeast-1
output = json

[profile my-iam-role]
region = ap-northeast-1
role_arn = arn:aws:iam::YYYYYYYYYYY:role/my-iam-role
source_profile = cm-shioya.hiromu
mfa_serial = arn:aws:iam::XXXXXXXXXXX:mfa/aws-iam-user

バケットの中身を別のバケットに再帰的にコピー

AWS CLI での高レベル (S3) コマンドの使用 - AWS Command Line Interface

現状

% aws s3 ls
2021-08-24 12:43:03 kwappa.dev

バケットを作成

% aws s3 mb s3://oldsite.kwappa.dev
make_bucket: oldsite.kwappa.dev
% aws s3 ls
2021-08-24 12:43:03 kwappa.dev
2021-08-24 15:34:49 oldsite.kwappa.dev

kwappa.devoldsite.kwappa.dev へコピー

% aws s3 cp s3://kwappa.dev/ s3://oldsite.kwappa.dev/ --recursive
copy: s3://kwappa.dev/base/0.png to s3://oldsite.kwappa.dev/base/0.png
copy: s3://kwappa.dev/about.html to s3://oldsite.kwappa.dev/about.html

  ... (snip)

copy: s3://kwappa.dev/recruit.html to s3://oldsite.kwappa.dev/recruit.html
copy: s3://kwappa.dev/images/sample2.jpg to s3://oldsite.kwappa.dev/images/sample2.jpg
% aws s3 ls s3://oldsite.kwappa.dev
                           PRE base/
                           PRE css/
                           PRE images/
                           PRE js/
2021-08-24 15:37:40      19082 about.html
2021-08-24 15:37:40       6105 confirm.html
2021-08-24 15:37:40       6150 contact.html
2021-08-24 15:37:41       5257 finish.html
2021-08-24 15:37:41       6345 form.html
2021-08-24 15:37:41      10266 index.html
2021-08-24 15:37:41       6369 recruit.html
2021-08-24 15:37:41       6156 works.html
ログインするとコメントできます