AWS Lambda:errorMessage流程在完成请求之前已退出

时间:2018-11-12 04:09:11

标签: android aws-lambda

嗨,我是android的新手!

我想将图像文件从android客户端上传到服务器(服务器制作缩略图,并返回缩略图的url)。

但是我仍然停留在此错误消息中。

{"errorMessage":"RequestId: 8e2a21b8-e62e-11e8-8585-d9b6fdfec9b9 Process exited before completing request"}!

我试图在stackoverflow中找到此错误代码,但找不到Android的答案。

请帮助或给我链接,我可以在那里解决这个问题...

这是服务器代码。

const AWS = require('aws-sdk');
const multipart = require("parse-multipart");
const s3 = new AWS.S3();
const bluebird = require('bluebird');

exports.handler = function(event, context) {
    let result = [];
    const bodyBuffer = new Buffer(event['body-json'].toString(), 'base64');
    const boundary = multipart.getBoundary(event.params.header['Content-Type']);
    const parts = multipart.Parse(bodyBuffer, boundary);
    const files = getFiles(parts);

    return bluebird.map(files, file => {
        console.log('UploadCall');
        return upload(file)
        .then(
            data => {
                result.push({
                    'bucket': data.Bucket,
                    'key': data.key,
                    'fileUrl': file.uploadFile.fullPath })
                console.log( `DATA => ${JSON.stringify(data, null, 2 )}`);
                },
                err => {
                    console.log(`S3 UPLOAD ERR => ${err}`);
                }
            )
        })
        .then(_=> {
            return context.succeed(result);
        });
    }

    let upload = function(file) {
        console.log('PutObject Call')
        return s3.upload(file.params).promise();
    };

    let getFiles = function(parts) {
    let files = [];
    parts.forEach(part => {
        const buffer = part.data

        const fileName = part.filename;
        const fileFullName = fileName;

        const originBucket = 'dna-edge/images';
        const filefullPath = `https://s3.ap-northeast-2.amazonaws.com/${originBucket}/${fileFullName}`;

        const params = {
            Bucket: originBucket,
            Key: fileFullName,
            Body: buffer
        };

        const uploadFile = {
            size: buffer.toString('ascii').length,
            type: part.type,
            name: fileName,
            fullPath: filefullPath
        };
        files.push({ params, uploadFile })
    });
    return files;
};

这是客户端代码。(imgURL看起来像/storage/emulated/0/DCIM/img/1493742568136.jpg)

public static String requestHttpPostLambda(String url, String imgURL){

    /*
     await axios.post(`${AWS_LAMBDA_API_URL}?type=${type}`, formData,
{ headers: { 'Content-Type': 'multipart/form-data' }})
.then((response) => {result = response});
     */
    String result=null;
    try {
        HttpClient client = new DefaultHttpClient();
        String postURL = url;
        HttpPost post = new HttpPost(postURL);
        post.setHeader("Content-Type", "multipart/form-data");

        File file = new File(imgURL);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("image", new FileBody(file));
        post.setEntity(builder.build());

        HttpResponse responsePOST = client.execute(post);
        Log.e("HttpResponse", responsePOST.getStatusLine()+"");
        HttpEntity resEntity = responsePOST.getEntity();
        if (resEntity != null) {
            result = EntityUtils.toString(resEntity);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

1 个答案:

答案 0 :(得分:0)

欢迎来到stackoverflow。

由于某种原因,AWS不太适合更新文档,请不要使用context.succeed,请使用作为第三个参数传递的回调。

我也将转移到Node 8.10运行时,因为那样的话,您可以使用async / await而不是使用promise / then模式。

export default(event, context, callback) => {

    try {

        // do some stuff

        callback(null, SOME_VALID_HTTP_RESPONSE)
    } catch(e){

        callback(e, null)
    }
}

Lambda失败的原因有很多,如果该过程在完成之前退出,要么崩溃要么您没有返回有效的HTTP响应(如果您的lambda位于API网关后面)

两种解决方案-首先要看的地方是cloudwatch,找到您的lambda函数名称,并检查最新日志以查找错误消息。

第二步-签出我的答案here,因此当函数成功执行时,您需要向API网关返回有效的HTTP响应,因此从本质上讲,如果您从那里使用我的代码,则可以执行以下操作:

callback(null, responder.success({someJson: someValue}))

任何问题都让我知道:-)

编辑:我正在更新这个问题,我只是在为将多个文件上传到S3做一个例子!