【发布时间】:2021-01-22 23:00:52
【问题描述】:
嘿,我正在关注这篇博文 - https://www.serverless.com/blog/publish-aws-lambda-layers-serverless-framework,它使用 ffmpeg 从视频文件创建 gif。
我的文件结构 -
制作人
- 层
- ffmpeg 库
- handler.js
- serverless.yml
我的 handler.js 代码 -
const { spawnSync } = require("child_process");
const { readFileSync, writeFileSync, unlinkSync } = require("fs");
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
module.exports.mkgif = async (event, context) => {
if (!event.Records) {
console.log("not an s3 invocation!");
return;
}
for (const record of event.Records) {
if (!record.s3) {
console.log("not an s3 invocation!");
continue;
}
if (record.s3.object.key.endsWith(".gif")) {
console.log("already a gif");
continue;
}
// get the file
const s3Object = await s3
.getObject({
Bucket: record.s3.bucket.name,
Key: record.s3.object.key
})
.promise();
// write file to disk
writeFileSync(`/tmp/${record.s3.object.key}`, s3Object.Body);
// convert to gif!
spawnSync(
"/opt/ffmpeg/ffmpeg",
[
"-i",
`/tmp/${record.s3.object.key}`,
"-f",
"gif",
`/tmp/${record.s3.object.key}.gif`
],
{ stdio: "inherit" }
);
// read gif from disk
const gifFile = readFileSync(`/tmp/${record.s3.object.key}.gif`);
// delete the temp files
unlinkSync(`/tmp/${record.s3.object.key}.gif`);
unlinkSync(`/tmp/${record.s3.object.key}`);
// upload gif to s3
await s3
.putObject({
Bucket: record.s3.bucket.name,
Key: `${record.s3.object.key}.gif`,
Body: gifFile
})
.promise();
}
};
我的 serverless.yml -
service: gifmaker
frameworkVersion: "2"
provider:
name: aws
runtime: nodejs12.x
region: ap-south-1
iamRoleStatements:
- Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
Resource: "arn:aws:s3:::${self:custom.bucket}/*"
functions:
mkgif:
handler: handler.mkgif
events:
- s3: ${self:custom.bucket}
layers:
- {Ref: FfmpegLambdaLayer}
layers:
ffmpeg:
path: layer
custom:
bucket: ${env:BUCKET, 'newsgator-company4'}
收到此错误 -
2020-10-07T22:32:14.695Z 13d283af-13dd-40e4-ae52-e1fc0d41f547 ERROR Invoke Error
{
"errorType": "Error",
"errorMessage": "ENOENT: no such file or directory, open '/tmp/data_store/0009.mp4'",
"code": "ENOENT",
"errno": -2,
"syscall": "open",
"path": "/tmp/data_store/0009.mp4",
"stack": [
"Error: ENOENT: no such file or directory, open '/tmp/data_store/0009.mp4'",
" at Object.openSync (fs.js:462:3)",
" at writeFileSync (fs.js:1362:35)",
" at Runtime.module.exports.mkgif [as handler] (/var/task/handler.js:29:5)",
" at runMicrotasks (<anonymous>)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)"
]
}
我已经坚持了几个小时了,无法弄清楚问题所在。 lambda 函数也具有管理员角色。
【问题讨论】:
-
mp4 有多大?
-
@mmason33 我上传的 mp4 文件是 8.2 MB
-
我认为这可能是内存问题,但不是
/tmp有 500 MB 可用空间。 -
啊!不是内存问题我认为这可能是 lambda 的权限问题,但现在我已经赋予它管理员角色。
-
对不起,我没有别的东西了。我对 aws-cli 不是很熟悉。
标签: javascript node.js amazon-s3 aws-lambda