Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以在AWS lambda上使用wkhtmltopdf_Python_Amazon Web Services_Lambda - Fatal编程技术网

Python 是否可以在AWS lambda上使用wkhtmltopdf

Python 是否可以在AWS lambda上使用wkhtmltopdf,python,amazon-web-services,lambda,Python,Amazon Web Services,Lambda,有一些节点Js github repo用于此。我是Node的新手,因此我使用python进行了尝试 问题 我在lambda上遇到权限被拒绝的错误。下面是我的代码以及错误代码 错误 { "errorMessage": "[Errno 13] Permission denied", "errorType": "PermissionError", "stackTrace": [ [ "/var/task/htmlToPdf.py"

有一些节点Js github repo用于此。我是Node的新手,因此我使用python进行了尝试

问题

我在lambda上遇到权限被拒绝的错误。下面是我的代码以及错误代码

错误

{
      "errorMessage": "[Errno 13] Permission denied",
      "errorType": "PermissionError",
      "stackTrace": [
        [
          "/var/task/htmlToPdf.py",
          50,
          "handler",
          "c=pdfkit.from_string(document,False,configuration=config )"
        ],
        [
          "/var/task/pdfkit/api.py",
          72,
          "from_string",
          "return r.to_pdf(output_path)"
        ],
        [
          "/var/task/pdfkit/pdfkit.py",
          129,
          "to_pdf",
          "stderr=subprocess.PIPE)"
        ],
        [
          "/var/lang/lib/python3.6/subprocess.py",
          707,
          "__init__",
          "restore_signals, start_new_session)"
        ],
        [
          "/var/lang/lib/python3.6/subprocess.py",
          1326,
          "_execute_child",
          "raise child_exception_type(errno_num, err_msg)"
        ]
      ]
    }
代码

import boto3
import os,subprocess
from boto3.s3.transfer import S3Transfer
from bs4 import BeautifulSoup
import time
import pdfkit
import codecs

LAMBDA_TASK_ROOT = os.environ.get('LAMBDA_TASK_ROOT', os.path.dirname(os.path.abspath(__file__)))
CURR_BIN_DIR = os.path.join(LAMBDA_TASK_ROOT, 'bin')
LIB_DIR = os.path.join(LAMBDA_TASK_ROOT, 'lib')
BIN_DIR = '/tmp/bin'

def _init_bin(executable_name,bucket_name,region):
    start = time.clock()
    if not os.path.exists(BIN_DIR):
        print("Creating bin folder")
        os.makedirs(BIN_DIR)
    print("Copying binaries for "+executable_name+" in /tmp/bin")
    client = boto3.client('s3', aws_access_key_id='xxxxxx',
                          aws_secret_access_key='xxxxxx',
                          region_name=region
                          )
    client.download_file(bucket_name,
                         'wkhtmltopdf', '/tmp/bin/wkhtmltopdf')
    newfile  = os.path.join(BIN_DIR, executable_name)
    print("Giving new binaries permissions for lambda")
    os.chmod(newfile, 777)
    elapsed = (time.clock() - start)
    print(executable_name+" ready in "+str(elapsed)+'s.')


def handler(event, context):
    bucket_name = event['bucketName']
    region = event['regionName']
    htmlFileName = event['htmlFileName']
    pdfName = htmlFileName.split(".")
    client = boto3.client('s3', aws_access_key_id='xxxxxxxxxxxxxxxxx',
                          aws_secret_access_key='xxxxxxxx',
                          region_name=region
                          )
    client.download_file(bucket_name,
                         htmlFileName, '/tmp/' + htmlFileName)
    _init_bin('wkhtmltopdf',event['bucketName'],event['regionName'])
    f=codecs.open('/tmp/'+htmlFileName,'r','utf-8')
    document=BeautifulSoup(f.read()).get_text()
    path_wkthmltopdf ='/tmp/bin/wkhtmltopdf'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    c=pdfkit.from_string(document,False,configuration=config )
    #transfer = S3Transfer(client)
    #transfer.upload_file('/tmp/' + pdfName[0] + '.pdf', bucket_name, pdfName[0] + '.pdf')
我尝试将wkhtmltopdf文件保存在zip包中,然后上载,但返回相同的错误


Aws lambda有可能吗?我已经在EC2实例中尝试过了,效果很好。

Python库
wkhtmltopdf
只是连接到
wkhtmltopdf
应用程序的桥梁/客户端,该应用程序必须安装在目标服务器/系统上。我也面临同样的问题。你能得到任何答案吗?你需要在对所有文件执行chmod后压缩lambda包。在这之后,它对我起了作用。