简体   繁体   中英

Unexpected Token s3 error on AWS Lambda Node 12.X

I'm using Node 12.x version to write my Lambda function. Here is the Parsing error that I am getting. What could be the reason? 解析错误:意外的令牌 S3

Update

const im = require("imagemagick");
const fs = require("fs");
const os = require("os");
const uuidv4 = require("uuid/v4");
const {promisify} = require("util");
const AWS = require('aws-sdk');

const resizeAsync = promisify(im.resize)
const readFileAsync = promisify(fs.readFile)
const unlinkAsync = promisify(fs.unlink)


AWS.config.update({region: 'ap-south-1'})
const s3 = new AWS.S3();

exports.handler = async (event) => {
    let filesProcessed = event.Records.map((record) => {
        let bucket = record.s3.bucket.name;
        let filename = record.s3.object.key;
    //Fetch filename from S3
    var params = {
        Bucket: bucket,
        Key: filename
    };
    //let inputData = await s3.getObject(params).promise()
    let inputData = await s3.getObject(params).promise();
    //Resize the file
    let tempFile = os.tmpdir() + '/' + uuidv4() + '.jpg';
    let resizeArgs = {
        srcData: inputData.Body,
        dstPath: tempFile,
        width: 150
    };
    await resizeAsync(resizeArgs)
    //Read the resized File
    let resizedData = await readFileAsync(tempFile)

    //Upload the resized file to S3
    let targetFilename = filename.substring(0, filename.lastIndexOf('.') + '-small.jpg')
    var params = {
        Bucket: bucket + '-dest',
        Key: targetFilename,
        Body: new Buffer(resizedData),
        ContentType: 'image/jpeg'
    }
    await s3.putObject(params).promise();
    return await unlinkAsync(tempFile)
})

await Promise.all(filesProcessed)
return "done"

}

Here is the same code. I am getting Unexpected token S3 error when hovering the red mark (shown in the image)

What you can do is, declare inputData as below and initialize it with the response from the getObject.

let inputData;    
var params = {
    Bucket: "examplebucket", 
    Key: "HappyFace.jpg"
};
s3.getObject(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     inputData = data;           // successful response
});

For more, you can refer here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM