簡體   English   中英

AWS lambda 無服務器中不存在此類文件錯誤

[英]AWS lambda no such file exists error in serverless

嘿,我正在關注這篇博文 - https://www.serverless.com/blog/publish-aws-lambda-layers-serverless-framework ,它使用 ffmpeg 從視頻文件創建 gif。

我的文件結構 -

動圖制作者

    • 圖書館ffmpeg
  • 處理程序.js
  • 無服務器.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 function 也具有管理員角色。

正如我所看到的問題是你給出了一條不存在的路徑

writeFileSync(`/tmp/${record.s3.object.key}`, s3Object.Body);

this becomes -> /tmp/data_store/0009.mp4

如果您的record.s3.object.key只是0009.mp4並且您的路由是/tmp/0009.mp4 ,那么您的解決方案將起作用 -> 這將起作用,因為如果文件不存在但在您的文件中, writeFileSync可以創建一個文件在這種情況下,這是不可能的,因為您沒有目錄data_store並且因為這不起作用。

暫無
暫無

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

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