

AWS LambdaでS3からGetObjectを実行するとエラーが発生し対処法が分かりません.

エラー内容

[code]

    {
      "errorMessage": "An error occurred (AccessDenied) when calling the GetObject operation: Access Denied",
      "errorType": "ClientError",
      "stackTrace": [
        [
          "/var/task/lambda_function.py",
          24,
          "lambda_handler",
          "raise e"
        ],
        [
          "/var/task/lambda_function.py",
          18,
          "lambda_handler",
          "response = s3.get_object(Bucket=bucket, Key=key)"
        ],
        [
          "/var/runtime/botocore/client.py",
          312,
          "_api_call",
          "return self._make_api_call(operation_name, kwargs)"
        ],
        [
          "/var/runtime/botocore/client.py",
          601,
          "_make_api_call",
          "raise error_class(parsed_response, operation_name)"
        ]
      ]
    }
    
[/code]

S3のポリシー

[code]

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AllowPublicRead",
                "Effect": "Allow",
                "Principal": "*",
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::fisourceimages/*"
            }
        ]
    }
    
[/code]

lambda関数

[code]

    import json
    import urllib.parse
    import boto3
    
    print('Loading function')
    
    s3 = boto3.client('s3')
    
    
    def lambda_handler(event, context):
        #print("Received event: " + json.dumps(event, indent=2))
    
        # Get the object from the event and show its content type
        bucket = event['Records'][0]['s3']['bucket']['name']
        key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
        try:
            response = s3.get_object(Bucket=bucket, Key=key)
            print("CONTENT TYPE: " + response['ContentType'])
            return response['ContentType']
        except Exception as e:
            print(e)
            print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
            raise e
    
[/code]

aws初心者のため、質問内容が適切かも判断つきません。  
どなたかご教授お願い致します。


CC BY-SA 3.0
2017-09-23T08:38:39.450
38139
2022-10-26T14:47:22.597
2022-10-26T14:47:22.597
19110
24920
question
aws amazon-s3 aws-lambda
AWS LambdaでS3からGetObjectを実行するとエラーが発生し対処法が分かりません


S3バケットのポリシーは基本的にHTTPリクエストを行う際のセキュリティ制御だったような気がします。  
Lambdaはファンクション登録時にそのファンクションで利用するIAMロールを選択できると思うので、そこでS3アクセス権限のあるロールを選択してください。  
尚、そういったロールが存在しない場合は作成する必要があります。  
<http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-s3-example-create-iam-
role.html>


CC BY-SA 3.0
2017-09-26T07:52:56.517
38204
2017-09-26T07:52:56.517
25536
38139
answer