AWS LambdaでS3からGetObjectを実行するとエラーが発生し対処法が分かりません
AWS LambdaでS3からGetObjectを実行するとエラーが発生し対処法が分かりません.
エラー内容
{
"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)"
]
]
}
S3のポリシー
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::fisourceimages/*"
}
]
}
lambda関数
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
aws初心者のため、質問内容が適切かも判断つきません。
どなたかご教授お願い致します。