如何在AWS Lambda函数中使用Sharp?我不断收到错误。 (node.js,用于调整图像大小)

问题描述 投票:0回答:1

我将此 Lambda 层添加到我的函数中,其描述如下:“此无服务器应用程序提供了一个 Lambda 层,其中包括 Node.js 的锐利图像处理库”

我现在尝试运行使用 Sharp 的 Lambda 函数,但我不断收到错误。

当我尝试运行它时包括导入语句,就像这样......

import sharp from 'sharp';
...
await sharp(inputImagePath)
    .resize(desiredWidth, desiredHeight)
    .toFile(outputImagePath);

...我收到此错误消息:

{
  "errorType": "Error",
  "errorMessage": "Could not load the \"sharp\" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n    npm install --include=optional sharp\n    yarn add sharp --ignore-engines\n- Ensure your package manager supports multi-platform installation:\n    See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n    npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n    See https://sharp.pixelplumbing.com/install",
  "trace": [
    "Error: Could not load the \"sharp\" module using the linux-x64 runtime",
    "Possible solutions:",
    "- Ensure optional dependencies can be installed:",
    "    npm install --include=optional sharp",
    "    yarn add sharp --ignore-engines",
    "- Ensure your package manager supports multi-platform installation:",
    "    See https://sharp.pixelplumbing.com/install#cross-platform",
    "- Add platform-specific dependencies:",
    "    npm install --os=linux --cpu=x64 sharp",
    "- Consult the installation documentation:",
    "    See https://sharp.pixelplumbing.com/install",
    "    at Object.<anonymous> (/opt/nodejs/node_modules/sharp/lib/sharp.js:114:9)",
    "    at Module._compile (node:internal/modules/cjs/loader:1469:14)",
    "    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)",
    "    at Module.load (node:internal/modules/cjs/loader:1288:32)",
    "    at Module._load (node:internal/modules/cjs/loader:1104:12)",
    "    at Module.require (node:internal/modules/cjs/loader:1311:19)",
    "    at require (node:internal/modules/helpers:179:18)",
    "    at Object.<anonymous> (/opt/nodejs/node_modules/sharp/lib/constructor.js:10:1)",
    "    at Module._compile (node:internal/modules/cjs/loader:1469:14)",
    "    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)"
  ]
}

然后,当我尝试运行 Sharp 命令而没有任何明显的导入语句时,它会给我“sharp 未定义”错误。

我只是很困惑,因为我的理解是,Lambda 层使得我可以在我的 Lambda 函数中使用 Sharp?我在这里错过了什么或做错了什么?谢谢。

javascript node.js aws-lambda sharp
1个回答
0
投票

更新:

解决方案是不使用这个 Lambda 层。相反,我必须将 Sharp 与我的 index.mjs 文件一起打包在一个压缩文件夹中,并且我能够以这种方式触发它。还有一些奇怪的附加要求,例如,确保您以正确的方式安装 Sharp,以便它与您正在编写的 AWS Lambda 函数代码架构兼容。解决这个问题确实很困难,但如果您正在努力解决这个问题让 Lambda 层正常工作,对我来说解决方法是不使用 Lambda 层。

© www.soinside.com 2019 - 2024. All rights reserved.