你好,我有一个lambda函数(python3.6),它无法从S3中读取文件,尽管该lambda函数在拥有S3无限制权限的角色下运行(IAM策略如下)。
该Lambda函数仅尝试从S3中检索文件并将其写入临时位置。但是,在调用s3.Bucket()
时会被阻塞并超时(即使设置了几分钟的超时时间)。
真正奇怪的是,它在没有任何异常的情况下超时,并且没有拒绝对s3.Bucket()
的调用,也没有出现某种权限错误。
这很基础,但我不知道如何解决这个问题。
import boto3
from botocore import exceptions
def lambda_handler(event, context):
key = event['image']
bucket = event['bucket']
tempfile = '/tmp/%s-%s' % (bucket, key)
print('(p) bucket: %s::%s' % (bucket, key))
print('(p) tempfile: %s' % tempfile)
s3 = boto3.resource('s3')
print('(p) resource intiialized')
try:
b = s3.Bucket(bucket)
print('(p) bucket info: %s [%s]' % (b.name, b.creation_date))
b.download_file(prefixed_key, tempfile)
print('(p} file downloaded to %s' % tempfile)
except exceptions.ParamValidationError as e:
return {"statusCode": 400, "body": 'ParamValidationError: [%s]' % e}
except exceptions.ClientError as e:
message = '[%s]: [%s]' % (e.response['Error']['Code'], e.response['Error']['Message'])
return {"statusCode": e.response['ResponseMetadata']['HTTPStatusCode'], "body": message}
print('(p) image downloaded from s3 and stored at: %s' % tempfile)
return None
角色拥有的IAM策略如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my_bucket",
"arn:aws:s3:::my_bucket/*"
]
}
]
}
示例日志:
22:42:43
START RequestId: 61c60167-839d-11e7-97b1-a772bbde2609 Version: $LATEST
START RequestId: 61c60167-839d-11e7-97b1-a772bbde2609 Version: $LATEST
22:42:43
(p) bucket: my_bucket::my_key
22:42:43
(p) tempfile: /tmp/my_bucket/my_key
22:42:43
(p) resource intiialized
22:43:13
END RequestId: 61c60167-839d-11e7-97b1-a772bbde2609
END RequestId: 61c60167-839d-11e7-97b1-a772bbde2609