Lambda函数在上传到S3时超时

时间:2016-03-08 23:21:06

标签: python amazon-s3 aws-lambda

我有一个用python编写的lambda函数,它在mongodb中执行一些操作,然后它应该将函数的tmp文件夹中的图片上传到s3。该功能可在上传步骤中保持超时。

我将超时时间提高到2分钟,该功能具有S3和vpc权限。该功能只是超时。任何人都有任何想法的想法?

示例输入

#picturename should be created by the app. a name unique for the dish
{
    "UserId": "56dc63fc1769d032d4d78e2e",  
    "DishId": "56dcc2781769d032d4d78e2f",
    "PictureName" : "katsu-001.png",
    "Data": "base64 image just the bits ignore data:image/jpeg;base64, if you have it"
}

功能

def addPicture(event,context):  

from __future__ import print_function
import pymongo
from pymongo import MongoClient
import bson.code
from bson.objectid import ObjectId
import datetime
import json    
import boto3
import sys
import uuid
from base64 import decodestring

print ('Writing file to disk')
with open('/tmp/' + pictureName,"wb") as f:
    f.write(decodestring(event["Data"]))
    print ('File written to /tmp/' + pictureName)

s3_client = boto3.client('s3')
print ('Starting S3 upload')
bucket = "foundue-dev-filestore"
upload_path = 'pictures/dish/' + dishId.__str__() + '/' + pictureName
print ('Uploading /tmp/' + pictureName + ' ' + bucket + ' ' + upload_path) 
s3_client.upload_file('/tmp/' + pictureName,bucket, upload_path)   
print ('Upload Complete')  
#pics[pictureName] = upload_path
#dish["Pictures"] = pics
#dish["UpdatedOn"] = datetime.datetime.utcnow()
#db.dishes.replace_one({"_id": dishId}, dish)
return

附加到lambda的政策

oneClick_lambda_basic_vpc_execution_1457284829450
oneClick_lambda_s3_exec_role_1457392283800

输出

Loading function 
START RequestId: ed91c290-e582-11e5-95d6-ed4fc6a3321b Version: $LATEST 
Writing file to disk 
File written to /tmp/katsu-002png 
Starting S3 upload 
Uploading /tmp/katsu-002png foundue-dev-filestore pictures/dish/56dcc2781769d032d4d78e2f/katsu-002png 
END RequestId: ed91c290-e582-11e5-95d6-ed4fc6a3321b 
REPORT RequestId: ed91c290-e582-11e5-95d6-ed4fc6a3321b  Duration: 121003.49 ms  Billed Duration: 121000 ms Memory Size: 128 MB  Max Memory Used: 22 MB
2016-03-08T23:12:21.437Z ed91c290-e582-11e5-95d6-ed4fc6a3321b Task timed out after 121.00 seconds

2 个答案:

答案 0 :(得分:6)

真正的问题是lambda使用的是VPC,但是VPC没有端点来访问s3。 所以一定要确保你拥有它。

(并允许lambda足够的权限来调用s3)

现在它可以在不到一秒的时间内完成这些功能。

答案 1 :(得分:0)

您的输出足够清晰:

  

任务在121.00秒后超时

似乎2分钟还不足以成功上传您的文件。 尝试使用较小的文件重现该问题。您也可以尝试设置超时设置的最大值(5分钟)。