NodeJS aws cdk图形化敏捷lambda pdf到png

amrnrhlw  于 2022-12-12  发布在  Node.js
关注(0)|答案(1)|浏览(128)

我需要AWS CDK来部署一个lambda fleet,并希望使用gm将PDF的第一页转换为PNG。我对实现没问题,因为它在我的笔记本电脑上工作得很好,我只是需要帮助添加图形二进制文件。
我当然会得到Error: Stream yields empty buffer,因为下面的NodejsFunction没有包含必要的二进制文件。我如何部署安装了graphicsmagick的NodejsFunction?

const designerHandler = new NodejsFunction(this, "designer-server", {
      functionName: "designer-server",
      memorySize: 512,
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: "handler",
      entry: path.join(__dirname, "./server/src/index.ts"),
      timeout: cdk.Duration.seconds(30),
    });

我已经尝试过使用层没有成功,我现在正在考虑使用docker。

designerHandler.addLayers(
      lambda.LayerVersion.fromLayerVersionArn(
        this,
        "layer-graphicsmagick",
        "arn:aws:lambda:ap-southeast-2:391641713082:layer:graphicsmagick-layer:1"
      )
    );

下面是我对gm的用法。

const pngOf = (pdf: Buffer) => {
    return new Promise(
      (resolve: (value: Buffer) => void, reject: (reason: any) => void) => {
        gm(pdf)
          .density(600, 600)
          .resize(600)
          .toBuffer('PNG', (err, png) => {
            if (err) {
              reject(err);
            }
            resolve(png);
          });
      }
    );
  };
kmpatx3s

kmpatx3s1#

最后我用DockerImage函数解决了这个问题。

const designerHandler = new lambda.DockerImageFunction(
  this,
  "designer-server",
  {
    functionName: "designer-server",
    memorySize: 512,
    code: lambda.DockerImageCode.fromImageAsset(
      path.join(__dirname, "./server")
    ),
    timeout: cdk.Duration.seconds(30),
  }
);

Dockerfile

# transpile typescript into javascript
FROM public.ecr.aws/lambda/nodejs:18 as builder

WORKDIR /usr/app

COPY package*.json ./
RUN npm install

# .dockerignore node_modules/ dist/
COPY . .
# rimraf dist && tsc
RUN npm run build 

FROM public.ecr.aws/lambda/nodejs:18

WORKDIR ${LAMBDA_TASK_ROOT}

RUN yum -y install gcc-c++ make libpng-devel libjpeg-devel libtiff-devel wget tar gzip libpng libjpeg libtiff ghostscript freetype freetype-devel jasper jasper-devel
RUN wget https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.38/GraphicsMagick-1.3.38.tar.gz \
        && tar -zxvf GraphicsMagick-1.3.38.tar.gz \
        && rm GraphicsMagick-1.3.38.tar.gz
RUN ./GraphicsMagick-1.3.38/configure --prefix=/var/task/graphicsmagick --enable-shared=no --enable-static=yes
RUN make
RUN make install
RUN tar -zcvf ~/graphicsmagick.tgz /var/task/graphicsmagick/

ENV PATH="${PATH}:/var/task/graphicsmagick/bin"

COPY package*.json ./
RUN npm install

# copy builders javascript to task root
COPY --from=builder /usr/app/dist ./
ENTRYPOINT [ "/lambda-entrypoint.sh" ]
CMD [ "index.handler" ]

处理程序中的用法就像下面这样简单:

import gm from 'gm';

这是tsconfig.json以防万一。

{
  "compilerOptions": {
    "target": "es2020",
    "module": "commonjs",
    "rootDir": "./src",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "baseUrl": "./src",
    "paths": {
      "src/*": ["./*"]
    },
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

如果不使用typescript,则完全删除构建器,只需COPY usr/app/src ./

相关问题