AWS Lambda tar文件提取似乎不起作用 [英] AWS lambda tar file extraction doesn't seem to work

查看:98
本文介绍了AWS Lambda tar文件提取似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于本教程.这是完整的python lambda函数:

I'm trying to run serverless LibreOffice based on this tutorial. Here is the full python lambda function:

import boto3
import os

s3_bucket = boto3.resource("s3").Bucket("lambda-libreoffice-demo")
os.system("curl https://s3.amazonaws.com/lambda-libreoffice-demo/lo.tar.gz -o /tmp/lo.tar.gz && cd /tmp && tar -xf /tmp/lo.tar.gz")
convertCommand = "instdir/program/soffice --headless --invisible --nodefault --nofirststartwizard --nolockcheck --nologo --norestore --convert-to pdf --outdir /tmp"

def lambda_handler(event,context):
  inputFileName = event['filename']
  # Put object wants to be converted in s3
  with open(f'/tmp/{inputFileName}', 'wb') as data:
      s3_bucket.download_fileobj(inputFileName, data)

  # Execute libreoffice to convert input file
  os.system(f"cd /tmp && {convertCommand} {inputFileName}")

  # Save converted object in S3
  outputFileName, _ = os.path.splitext(inputFileName)
  outputFileName = outputFileName  + ".pdf"
  f = open(f"/tmp/{outputFileName}","rb")
  s3_bucket.put_object(Key=outputFileName,Body=f,ACL="public-read")
  f.close()

运行完整脚本时的响应是:
"errorMessage":"ENOENT:没有这样的文件或目录,请打开'/tmp/example.pdf'",

The response when running the full scripts is:
"errorMessage": "ENOENT: no such file or directory, open '/tmp/example.pdf'",

所以我开始逐行调试它.
根据我的调试打印,当尝试在第二行中提取二进制文件时,它似乎在一开始就失败了:

So I began to debug it row by row.
Based on my debug prints, it seems that it fails right on the start, when trying to extract the binary on the second row:

os.path.exists('/tmp/lo.tar.gz') // => true
os.path.exists('/tmp/instdir/program/soffice.bin') // => false

所以看起来焦油是那里有问题的部分.如果我从S3下载文件并在本地运行 tar 命令,则似乎可以很好地提取文件.

So it looks like the tar is the problematic part there. If I download the file from S3 and run the tar command locally it seems to extract the file just fine.

尝试使用node,python 3.8,python 3.6.还尝试了有无层(以及/opt/lo.tar.br 路径)作为在此描述.

Tried with node, python 3.8, python 3.6. Also tried it with and without the layer (and the /opt/lo.tar.br path) as described here.

推荐答案

我遇到了同样的问题.

我怀疑问题是在/tmp中执行文件时出现权限错误.

I suspect the problem is a permissions error executing files in /tmp.

尝试将 instdir/复制到您的主文件夹&从那里跑出来.

Try copying instdir/ to your home folder & running it out of there.

请写回确认您是否对此进行了测试!

Please write back to confirm if you test this!

我最终创建了一个可以正确安装LibreOffice的Docker容器,例如:

I ended up creating a Docker container which installs LibreOffice properly, e.g.:

# Use Amazon Linux 2 (It's based on CentOS) as base image
FROM amazon/aws-lambda-provided:al2

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Download and install LibreOffice (and deps)

RUN yum update -y \
    && yum clean all \
    && yum install -y wget tar gzip

RUN cd /tmp \
    && wget http://download.documentfoundation.org/libreoffice/stable/7.0.4/rpm/x86_64/LibreOffice_7.0.4_Linux_x86-64_rpm.tar.gz \
    && tar -xvf LibreOffice_7.0.4_Linux_x86-64_rpm.tar.gz

# For some reason we need to "clean all"
RUN cd /tmp/LibreOffice_7.0.4.2_Linux_x86-64_rpm/RPMS \
    && yum clean all \
    && yum -y localinstall *.rpm 

# Required deps for soffice
RUN yum -y install \
    fontconfig libXinerama.x86_64 cups-libs dbus-glib cairo libXext libSM libXrender

# NOTE: Should we install libreoffice-writer? (doesn't seem to be required)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# We need to read/write to S3 bucket
RUN yum -y install \
    awscli \
    jq

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# We test with this file
COPY test-template.docx /home/test-template.docx

# This code derives from Ari's original article
COPY process_doc.sh     /home/process_doc.sh
COPY bootstrap          /var/runtime/bootstrap
COPY function.sh        /var/task/function.sh

RUN chmod u+rx \
    /home/process_doc.sh \
    /var/runtime/bootstrap \
    /var/task/function.sh

CMD [ "function.sh.handler" ]
# ^ Why CMD not ENTRYPOINT

...并运行容器化的lambda: https://github.com/pi-/lambda-container-image-with-custom-runtime-example

... and running a containerized lambda: https://github.com/p-i-/lambda-container-image-with-custom-runtime-example

这篇关于AWS Lambda tar文件提取似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆