You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS Lambda Python压缩S3中PDF遇模块导入错误的排查求助

AWS Lambda 导入模块错误排查:No module named 'lambda_function'

问题概述

我尝试使用AWS Lambda的Python代码压缩S3存储桶testforbucket023中的test.pdf文件,并将压缩后的文件保存至另一个S3存储桶testforbucket023-compressed。已创建包含所需依赖(boto3、PyPDF2、typing_extensions、botocore)的部署包lambda_function.zip,但运行Lambda函数时出现如下错误:

"errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'", "errorType": "Runtime.ImportModuleError"

相关配置

  • 源码文件名:lambda_function
  • Lambda处理函数名:lambda_handler
  • Lambda处理程序配置:lambda_function.lambda_handler

修改后的Lambda代码

import boto3
import botocore
import PyPDF2
import io

def lambda_handler(event, context):
    # Retrieve the input and output S3 bucket names from the event
    input_bucket = event['testforbucket023']
    output_bucket = event['testforbucket023-compressed']

    # Retrieve the S3 object key for the input PDF file from the event
    key = event['test.pdf']

    # Create an S3 client
    s3 = boto3.client('s3')

    try:
        # Download the PDF file from the input S3 bucket
        response = s3.get_object(Bucket=input_bucket, Key=key)
        pdf_content = response['Body'].read()

        # Compress the PDF file
        compressed_pdf_content = compress_pdf(pdf_content)

        # Upload the compressed PDF file to the output S3 bucket
        compressed_key = f'compressed_{key}'
        s3.put_object(Body=compressed_pdf_content, Bucket=output_bucket, Key=compressed_key)

        return {
            'statusCode': 200,
            'body': f'Compressed PDF file uploaded as {compressed_key}'
        }
    except botocore.exceptions.ClientError as e:
        return {
            'statusCode': 500,
            'body': str(e)
        }

def compress_pdf(pdf_content):
    # Create a PDF reader
    pdf_reader = PyPDF2.PdfFileReader(io.BytesIO(pdf_content))

    # Create a PDF writer
    pdf_writer = PyPDF2.PdfFileWriter()

    # Iterate through each page of the PDF file
    for page_num in range(pdf_reader.getNumPages()):
        # Get the page
        page = pdf_reader.getPage(page_num)

        # Compress the page
        page.compressContentStreams()

        # Add the compressed page to the PDF writer
        pdf_writer.addPage(page)

    # Create an in-memory stream for the compressed PDF content
    compressed_pdf_stream = io.BytesIO()
    pdf_writer.write(compressed_pdf_stream)
    compressed_pdf_stream.seek(0)

    return compressed_pdf_stream.getvalue()

原始Lambda代码

import boto3
import botocore
import PyPDF2
import io

def lambda_handler(event, context):
    # Retrieve the input and output S3 bucket names from the event
    input_bucket = event['input_bucket']
    output_bucket = event['output_bucket']

    # Retrieve the S3 object key for the input PDF file from the event
    key = event['key']

    # Create an S3 client
    s3 = boto3.client('s3')

    try:
        # Download the PDF file from the input S3 bucket
        response = s3.get_object(Bucket=input_bucket, Key=key)
        pdf_content = response['Body'].read()

        # Compress the PDF file
        compressed_pdf_content = compress_pdf(pdf_content)

        # Upload the compressed PDF file to the output S3 bucket
        compressed_key = f'compressed_{key}'
        s3.put_object(Body=compressed_pdf_content, Bucket=output_bucket, Key=compressed_key)

        return {
            'statusCode': 200,
            'body': f'Compressed PDF file uploaded as {compressed_key}'
        }
    except botocore.exceptions.ClientError as e:
        return {
            'statusCode': 500,
            'body': str(e)
        }

def compress_pdf(pdf_content):
    # Create a PDF reader
    pdf_reader = PyPDF2.PdfFileReader(io.BytesIO(pdf_content))

    # Create a PDF writer
    pdf_writer = PyPDF2.PdfFileWriter()

    # Iterate through each page of the PDF file
    for page_num in range(pdf_reader.getNumPages()):
        # Get the page
        page = pdf_reader.getPage(page_num)

        # Compress the page
        page.compressContentStreams()

        # Add the compressed page to the PDF writer
        pdf_writer.addPage(page)

    # Create an in-memory stream for the compressed PDF content
    compressed_pdf_stream = io.BytesIO()
    pdf_writer.write(compressed_pdf_stream)
    compressed_pdf_stream.seek(0)

    return compressed_pdf_stream.getvalue()

错误排查步骤建议

  • 检查部署包结构:解压lambda_function.zip,确认lambda_function.py直接位于压缩包的根目录,而非嵌套在子文件夹中。Lambda需要从根目录加载模块。
  • 确认文件名正确性:确保源码文件的完整名称是lambda_function.py,不能缺少.py后缀,也不能有拼写错误(比如大小写不一致、多/少字符)。
  • 验证打包方式:进入代码和依赖所在的目录,选中lambda_function.py以及所有依赖文件夹(如boto3、PyPDF2)一起压缩,不要把整个上级文件夹打包进去。
  • 匹配Python运行时版本:确保Lambda配置的Python运行时版本(比如3.9、3.10)和打包依赖时使用的本地Python版本完全一致,版本不匹配可能导致模块无法被识别。
  • 本地测试导入:在本地Python环境中,进入代码所在目录,执行import lambda_function命令,检查是否能成功导入,排除代码语法错误导致的导入失败。
  • 注意事件参数问题:修改后的代码中直接读取event['testforbucket023']是错误的,应该保留原代码的event['input_bucket']、event['output_bucket']、event['key'],测试时传入包含这些键的事件参数(这是后续功能问题,但需提前修正)。

内容的提问来源于stack exchange,提问作者raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:27:50