使用 Sharp 庫建置節點時出現 AWS Lambda 錯誤

使用 Sharp 庫建置節點時出現 AWS Lambda 錯誤

我在建立 lambda 處理程序時遇到問題。我正在嘗試使用 Sharp 庫在 S3 上調整圖像大小。

我的程式碼:

S3.getObject({
    Bucket: bucketName,
    Key: objectKey
}, (err, data) => {
    SHARP(data.Body).resize(250, 250).toFormat("jpg").toBuffer().then((buffer) => {
        S3.putObject({
            Body: buffer,
            Bucket: bucketName,
            Key: objectKey,
            ContentType: "image/jpeg",
            ContentDisposition: "inline",
        })
    })
});

我的節點相依性:

  "dependencies": {
    "aws-sdk": "^2.637.0",
    "dotenv": "^10.0.0",
    "mysql": "^2.18.1",
    "sharp": "^0.29.1",
    "util": "^0.12.4"
  },
  "devDependencies": {
    "esbuild": "^0.12.27",
    "nodemon": "^2.0.12"
  }

我要建立的程式碼:

esbuild --bundle --minify --platform=node --target=node12 --outdir=build index.js && cd build && zip index.js.zip index.js

AWS Lambda 錯誤:

Cannot find module '../build/Release/sharp-linux-x64.node'
Require stack:
- /var/task/index.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js

Possible solutions:
- Install with the --verbose flag and look for errors: "npm install --ignore-scripts=false --verbose sharp"
- Install for the current runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install

相關內容