AWS Lambda在无服务器中不存在此类文件错误

时间:2020-10-07 22:44:41

标签: javascript node.js amazon-s3 aws-lambda

嘿,我正在关注此博客文章-https://www.serverless.com/blog/publish-aws-lambda-layers-serverless-framework,该文章使用ffmpeg从视频文件创建gif。

我的文件结构-

gifmaker

    • 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函数还具有管理员角色。

1 个答案:

答案 0 :(得分:0)

我看到的问题是您提供的路径不存在

setUpClass()

如果您的writeFileSync(`/tmp/${record.s3.object.key}`, s3Object.Body); this becomes -> /tmp/data_store/0009.mp4 仅为record.s3.object.key,则您的解决方案将起作用 并且您的路由将为0009.mp4->之所以可行,是因为/tmp/0009.mp4可以创建一个文件(如果该文件不存在),但是在您的情况下,这是不可能的,因为您没有目录{{1 }},因为这行不通。