我在AWS上使用库调用mmmagic创建一个Lambda函数,以检查上传的S3文件事件中的mimeType。使用模拟测试事件在我的机器上一切正常。我确信我将node_modules
内的所有依赖项上传到Lambda。
这是来自Lambda的错误消息:
module initialization error: Error
at Error (native)
at Object.Module._extensions..node (module.js:597:18)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous>
(/var/task/node_modules/mmmagic/lib/index.js:3:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
这是我的代码:
const aws = require('aws-sdk')
const mmagic = require('mmmagic');
const s3 = new aws.S3();
exports.handler = (event, context, callback) => {
const params = {
Bucket: event.Records[0].s3.bucket.name,
Key: event.Records[0].s3.object.key
}
s3.getObject(params, (err, data) => {
if (err) console.log(err, err.stack) // an error occurred
else {
var magic = new mmagic.Magic();
magic.detect(data.Body, function (err, result) {
if (err) {
console.log('Error when try getting mime type:' + err) // logherer
} else {
// do something
}
}
});
}
})
}
的package.json
{
"aws-sdk": "^2.188.0",
"mmmagic": "^0.4.6"
}