LoginSignup
19
18

More than 5 years have passed since last update.

aws s3 cp filename s3://bucket/path/to/filename に必要な IAM Policy

Posted at

Jenkins からファイルを S3 に投げ込みたいと思ったので、専用の IAM User を作って、権限を設定しようと思いました。とりあえず PutObject だけでー

{
  "Version": "2012-10-17",
  "Statement": [
   {
      "Sid": "PutObject",
      "Effect": "Allow",
      "Action": [
        "s3:PutObject"
      ],
      "Resource": [
        "arn:aws:s3:::bucket/path/to/*"
      ]
    }
  ]
}

と思ったら無念の Access Denied。

$ aws s3 cp filename s3://bucket/path/to/filename
A client error (AccessDenied) occurred: Access Denied

aws-cli が中でなにかやってるんですかね。こういう時は --debug オプションをつけて実行。

$ aws s3 cp filename s3://bucket/path/to/filename --debug
2013-12-10 16:34:45,638 - botocore.hooks - DEBUG - Event building-parameter-table.s3.cp: calling handler <function add_cmd_params at 0x00000000032CF908>
(…… 中略 ……)
2013-12-10 16:34:45,670 - botocore.operation - DEBUG - Operation:ListObjects called with kwargs: {'max_keys': 0, 'bucket': u'bucket'}
(…… 後略 ……)

どうやら ListObject なる操作をしているようです。しかしそんな権限は S3 の Actions 一覧には存在しないので、代わりに ListBucket を許可してみます。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ListBucket",
      "Effect": "Allow",
      "Action": [
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::bucket"
      ]
    }, {
      "Sid": "PutObject",
      "Effect": "Allow",
      "Action": [
        "s3:PutObject"
      ],
      "Resource": [
        "arn:aws:s3:::bucket/path/to/*"
      ]
    }
  ]
}

これで試してみると

$ aws s3 cp filename s3://bucket/path/to/filename
upload: .\filename to s3://bucket/path/to/filename

できた!!

19
18
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
19
18