这里有一些Node Js github repos。我是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实例尝试了这个,它运行正常。