簡體   English   中英

AWS Lambda Node 12.X 上的意外令牌 s3 錯誤

[英]Unexpected Token s3 error on AWS Lambda Node 12.X

我正在使用 Node 12.x 版本來編寫我的 Lambda 函數。 這是我得到的解析錯誤。 可能是什么原因? 解析錯誤:意外的令牌 S3

更新

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"

}

這是相同的代碼。 懸停紅色標記時出現意外令牌 S3 錯誤(如圖所示)

您可以做的是,如下聲明 inputData 並使用來自 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
});

有關更多信息,您可以參考這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM