JooK
JooK

Reputation: 21

AWS Lambda : errorMessage Process exited before completing request

Hi I'm newbie in android!

I want to upload image file from android client to server(Server makes thumbnail, and return thumbnail's url).

However I stucked in this error message.

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

I tried to find this error code in stackoverflow, but i cannot found answer for android.

Please help or give me link where I can solve this problem...

Here is server code.

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;
};

And this is client code.(imgURL looks like /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;
}

Upvotes: 2

Views: 1574

Answers (1)

Mrk Fldig
Mrk Fldig

Reputation: 4486

Welcome to stackoverflow.

So for some reason AWS aren't too good an updating the docs, don't use context.succeed, use the callback thats passed as a third param.

Also I'd move to Node 8.10 runtime because then rather than using promises/then pattern you can use async/await.

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

    try {

        // do some stuff

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

        callback(e, null)
    }
}

There's a few reason your Lambda could be failing, if the process exited before completing it's either crashing OR you're not returning a valid HTTP response(if your lambda is behind API gateway)

Two solutions - first place to look is in cloudwatch, find your lambda function name and check the latest log to look for error messages.

Second - check out my answer here so when your function succeeds you need to return a valid HTTP response to API Gateway so in essence if you use my code from there you can do:

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

Any questions let me know :-)

EDIT: I'm updating this question I'm just working on an example for a multiple file upload to S3!

Upvotes: 2

Related Questions