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

AWS Lambda部署pyminizip模块失败问题求助

解决AWS Lambda Python3.9中pyminizip导入失败问题

核心原因

pyminizip依赖C编译的二进制扩展,本地(Windows/macOS)安装的版本和Lambda的Amazon Linux 2运行环境不兼容,导致无法识别模块。

解决方案一:在兼容环境中编译pyminizip

用Docker模拟Lambda的Python3.9运行环境,重新安装pyminizip:

  • 拉取Lambda官方镜像:
    docker pull public.ecr.aws/lambda/python:3.9
    
  • 挂载本地目录到容器并进入交互模式:
    docker run -v "$(pwd):/var/task" -it public.ecr.aws/lambda/python:3.9 bash
    
  • 在容器内安装pyminizip到当前目录:
    pip install pyminizip -t .
    
  • 退出容器后,将生成的pyminizip文件夹和你的Lambda代码一起打包部署,或者打包成Lambda Layer。

解决方案二:改用纯Python加密压缩库pyzipper

如果不想折腾编译环境,换用纯Python实现的pyzipper,无需C扩展,兼容性更强:

  • 安装pyzipper到部署目录:
    pip install pyzipper -t .
    
  • 示例代码逻辑(适配S3操作):
    import boto3
    import pyzipper
    from io import BytesIO
    
    s3 = boto3.client('s3')
    
    def lambda_handler(event, context):
        # 从源S3下载文件
        source_bucket = 'your-source-bucket'
        source_key = 'path/to/your/file'
        response = s3.get_object(Bucket=source_bucket, Key=source_key)
        file_content = response['Body'].read()
    
        # 创建加密ZIP
        zip_buffer = BytesIO()
        with pyzipper.AESZipFile(zip_buffer, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as zf:
            zf.setpassword(b'your-password')
            zf.writestr('filename-in-zip.txt', file_content)
    
        # 上传到目标S3
        target_bucket = 'your-target-bucket'
        target_key = 'path/to/encrypted/file.zip'
        s3.put_object(Bucket=target_bucket, Key=target_key, Body=zip_buffer.getvalue())
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:55:16