@alexey.noskov please see the full Error
[ERROR] KeyError: 'aspose.words'
Traceback (most recent call last):
File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/var/task/lambda_function.py", line 4, in <module>
import aspose.words as aw
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 674, in _load_unlocked
File "<frozen importlib._bootstrap>", line 642, in _load_backward_compatible
- Are you able to install the
aspose.words
package in the AWS Lambda environment?
I have added the Aspose.Words library to my Python code folder and zipped it. This zip file needs to be uploaded to Lambda.
- What OS is used in the AWS Lambda environment?
It uses a Linux OS.
- If you are able to install
aspose.words
and the problem occurs while running the code, please provide the code and input documents that will allow us to reproduce the problem.
It is not even reaching the code execution; it stops at the import stage. I will provide my sample code.Preformatted text
import json
import boto3
import os
import aspose.words as aw
from botocore.exceptions import NoCredentialsError
def lambda_handler(event, context):
# Load Aspose.Words license
license_path = '/tmp/Aspose.Words.lic'
s3 = boto3.client('s3')
s3.download_file('', '', license_path)
license = aw.License()
license.set_license(license_path)
# Extract bucket name and object key from the event
try:
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
except KeyError as e:
return {
'statusCode': 400,
'body': json.dumps(f'Error extracting S3 bucket details: {e}')
}
# Download the RTF file from S3
input_path = '/tmp/input.rtf'
try:
s3.download_file(bucket_name, object_key, input_path)
except NoCredentialsError:
return {
'statusCode': 400,
'body': json.dumps('Credentials not available')
}
# Convert RTF to PDF using Aspose.Words
doc = aw.Document(input_path)
pdf_path = '/tmp/output.pdf'
doc.save(pdf_path, aw.SaveFormat.PDF)
# Upload the PDF back to the S3 bucket
pdf_key = object_key.replace('.rtf', '.pdf')
s3.upload_file(pdf_path, bucket_name, pdf_key)
return {
'statusCode': 200,
'body': json.dumps('RTF converted to PDF successfully')
}