简体   繁体   English

节点ImageMagick在本地成功但在AWS Lambda中失败

[英]Node ImageMagick succeed locally but fails in AWS Lambda

I'm using this Node ImageMagick for validating images via converting them in AWS Lambda function. 我正在使用此Node ImageMagick通过在AWS Lambda函数中转换它们来验证图像。 Locally I have no problem, but when I deploy my function I get the error no decode delegate for this image format on some images only : 本地我没有问题,但是当我部署我的函数时,我只在某些图像上获得了no decode delegate for this image format的错误no decode delegate for this image format

{ Error: Command failed: convert: no decode delegate for this image format `/tmp/925bf249f8297827f51f0370642eb560.jpg' @ error/constitute.c/ReadImage/544.
convert: no images defined `/tmp/5d2baeb2-de13-4868-a970-ad919c609440.png' @ error/convert.c/ConvertImageCommand/3046.

at ChildProcess.<anonymous> 
(/var/task/node_modules/imagemagick/imagemagick.js:88:15)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:886:16)
at Socket.<anonymous> (internal/child_process.js:342:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:497:12) timedOut: false, killed: false, code: 1, signal: null }

This is after I failed to use ImageMagick that's built-in for AWS Lambda. 这是在我未能使用内置于AWS Lambda的ImageMagick之后。 Any idea how I can solve this? 知道如何解决这个问题吗?

Finally I figured it out! 最后我想通了! There is no short way for this. 这没有简短的方法。 This is what I ended up doing: 这就是我最终做的事情:

  • I ran Parallel on my Mac and I got the binaries of ImageMagick installed on a virtual CentOS machine. 我在Mac上运行了Parallel ,我在虚拟CentOS机器上安装了ImageMagick的二进制文件。 To install the binaries I followed the guides in the official website . 要安装二进制文件,我按照官方网站上的指南进行操作。 I wrote a lot of commands in my virtual machine but I can summarise them in: 我在我的虚拟机中编写了很多命令,但我可以将它们总结为:

     yum install sudo -y sudo yum -y install libpng-devel libjpeg-devel libtiff-devel gcc sudo curl -O http://www.imagemagick.org/download/ImageMagick.tar.gz sudo yum install tar -y sudo tar zxvf ImageMagick.tar.gz cd ImageMagick-7.0.8-22 sudo ./configure --prefix=/var/task/imagemagick --enable-shared=no --enable-static=yes sudo yum install make -y sudo yum install automake autoconf libtool -y sudo sed -i 's|(gitversion|(./gitversion|' configure.ac sudo yum install git -y sudo autoreconf -i sudo sed -i '$a LANG=en_US.utf-8' /etc/environment sudo sed -i '$a LC_ALL=en_US.utf-8' /etc/environment sudo make sudo make install tar zcvf ~/imagemagick.tgz /var/task/imagemagick/` 
  • Then I copied the installation folder from the virtual machine into a folder I named /lib in my root directory of AWS Lambda repository. 然后我将安装文件夹从虚拟机复制到我在AWS Lambda存储库的根目录中命名为/lib的文件夹中。

  • Then in order to use the new binaries, I needed to copy ImageMagick folder from /lib into my node_modules on every deployment. 然后,为了使用新的二进制文件,我需要在每次部署时将/lib ImageMagick文件夹复制到我的node_modules This is because AWS Lambda didn't allow me to access any binary file from the /lib directly for unknown reason. 这是因为AWS Lambda不允许我出于未知原因直接从/lib访问任何二进制文件。 In order to perform the copy on every deployment I added the following line to my deploy.sh file: 为了在每个部署上执行复制,我deploy.sh下行添加到deploy.sh文件中:

     `cp -R ./lib/imagemagick ./node_modules/imagemagick` 
  • Now the binaries are ready to be used, but still AWS Lambda didn't give me permission to run any command directly from within node_modules . 现在可以使用二进制文件,但AWS Lambda仍然不允许我直接从node_modules运行任何命令。 So every time I needed to run a particular command I needed to copy it (it is a binary file) into the /tmp folder and then I needed to change its mode chmod to be able to run it. 因此,每次我需要运行一个特定的命令时,我需要将它(它是一个二进制文件)复制到/tmp文件夹中,然后我需要更改其模式chmod才能运行它。 This is the code for every needed command (in Node): 这是每个所需命令的代码(在Node中):

     const command = '/node_modules/imagemagick/[command path and file]'; execSync(`cp -a ${command} /tmp/`); fs.chmodSync(command, 755); execSync(`chmod +x ${command}`); 
  • Now the command is ready to be used with child_process . 现在该命令已准备好与child_process一起使用。 For example: 例如:

     const argus = [originalImage, '-o', newImage]; child_process.execFile(command, argus, (err, stdout, stderr) ( if (err) throw err; console.log('IMAGE CONVERTED'); )} 
  • The above applies not only on ImageMagick, but on any other binary needed with AWS Lambda. 以上内容不仅适用于ImageMagick,也适用于AWS Lambda所需的任何其他二进制文件。 I applied the same on Google's WebP library somewhere else in my function. 我在我的函数中的其他地方的Google的WebP库中应用了相同的内容。 I downloaded its Linux binaries from Google developers website. 我从谷歌开发者网站下载了它的Linux二进制文件。

This is it. 就是这个。 My code works as expected. 我的代码按预期工作。 If you have any better idea on improving it I would appreciate a comment. 如果您对改进它有任何更好的想法,我将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM