vladholubiev / serverless-libreoffice

Run LibreOffice in AWS Lambda to create PDFs & convert documents
https://vladholubiev.com/serverless-libreoffice
513 stars 75 forks source link

Can't start lambda #21

Closed BrunIF closed 5 years ago

BrunIF commented 5 years ago

Run command

aws lambda invoke --function-name convert-to-pdf \
>     --payload '{"filename":"mr.xlsx"}' output.txt && cat output.txt

and got the answer

{
    "FunctionError": "Unhandled",
    "ExecutedVersion": "$LATEST",
    "StatusCode": 200
}
{"errorMessage": "An error occurred (403) when calling the HeadObject operation: Forbidden", "errorType": "ClientError", "stackTrace": [["/var/task/lambda_function.py", 12, "lambda_handler", "s3_bucket.download_fileobj(inputFileName, data)"], ["/var/runtime/boto3/s3/inject.py", 720, "bucket_download_fileobj", "Callback=Callback, Config=Config)"], ["/var/runtime/boto3/s3/inject.py", 678, "download_fileobj", "return future.result()"], ["/var/runtime/s3transfer/futures.py", 73, "result", "return self._coordinator.result()"], ["/var/runtime/s3transfer/futures.py", 233, "result", "raise self._exception"], ["/var/runtime/s3transfer/tasks.py", 255, "_main", "self._submit(transfer_future=transfer_future, **kwargs)"], ["/var/runtime/s3transfer/download.py", 353, "_submit", "**transfer_future.meta.call_args.extra_args"], ["/var/runtime/botocore/client.py", 314, "_api_call", "return self._make_api_call(operation_name, kwargs)"], ["/var/runtime/botocore/client.py", 612, "_make_api_call", "raise error_class(parsed_response, operation_name)"]]}

The function was created manually and via CLI. The same problem.

BrunIF commented 5 years ago
import boto3
import os

s3_bucket = boto3.resource("s3").Bucket("my-bucket")
os.system("pip install boto3 && curl https://s3-us-west-2.amazonaws.com/my-bucket/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()
BrunIF commented 5 years ago

My mistake. I don't copy source file to S3.