0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[AWS] awscli: aws s3 mv コマンド実行に必要なIAMポリシ

Last updated at Posted at 2019-06-04

同一バケット内で aws s3 mv コマンドを実行する場合、S3的にはキーの変更だから PutObject だけでできるかな?
→ 確認したら DeleteObject も必要でしたという話。

mv元 mv先 必要なAction
S3 S3(同一バケット内) GetObject
DeleteObject
PutObject
S3 S3(別のバケット) GetObject(mv元)
DeleteObject(mv元)
PutObject(mv先)
S3 ローカル(ファイルシステム) GetObject
DeleteObject
ローカル(ファイルシステム) S3 PutObject

mv元と先で表のパターンが考えられますが、それぞれ以下の通りでした。

同一バケット内のmv

DeleteObject 無しでできるかな?とやってみたらエラーになりました。

$ aws s3 mv s3://<バケット名>/demo-file1.txt s3://<バケット名>/demo-file2.txt
move failed: s3://<バケット名>/demo-file1.txt to s3://<バケット名>/demo-file2.txt An error occurred (AccessDenied) when calling the DeleteObject

最低限のポリシとしては以下になります。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:DeleteObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::<バケット名>/*"
            ]
        }
    ]
}

これでmvできるようになりました。

$ aws s3 mv s3://<バケット名>/demo-file1.txt s3://<バケット名>/demo-file2.txt
move: s3://<バケット名>/demo-file1.txt to s3://<バケット名>/demo-file2.txt

別バケットへのmv

先ほど使ったActionのうちGetObject, DeleteObjectはmv元に、PutObjectはmv先に付与が必要です。以下がポリシです。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::<mv元バケット名>/*"
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::<mv先バケット名>/*"
            ]
        }
    ]
}

S3からローカル(ファイルシステム)へのmv

mv元バケットに対するポリシが必要となります。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::<バケット名>/*"
            ]
        }
    ]
}

ローカル(ファイルシステム)からS3へのmv

同様に、mv先バケットに対するポリシが必要となります。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::<バケット名>/*"
            ]
        }
    ]
}

これだけのポリシではaws s3 lsコマンドも実行できないので、実際にはListBucketとか追加して使うと思いますが、最低限のポリシとしては以上です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?