pysftp库在AWS Lambda层中不起作用 [英] pysftp library not working in AWS lambda layer

查看:230
本文介绍了pysftp库在AWS Lambda层中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pysftp 库(Python脚本)将文件上传到EC2实例.所以我创建了一个小的Python脚本,该脚本使用下面的行进行连接

I want to upload files to EC2 instance using pysftp library (Python script). So I have created small Python script which is using below line to connect

pysftp.Connection(
    host=Constants.MY_HOST_NAME,
    username=Constants.MY_EC2_INSTANCE_USERNAME,
    private_key="./mypemfilelocation.pem",
)
some code here .....
pysftp.put(file_to_be_upload, ec2_remote_file_path)

此脚本将使用.pem文件将文件从本地Windows计算机上载到EC2实例,并且该脚本可以正常工作.

This script will upload files from my local Windows machine to EC2 instance using .pem file and it works correctly.

现在,我想使用具有API网关功能的 AWS lambda 来执行此操作.

Now I want to do this action using AWS lambda with API Gateway functionality.

因此,我已将Python脚本上传到AWS lambda.现在,我不确定如何在AWS Lambda中使用pysftp库,因此我找到了在AWS lambda Layer中添加pysftp库Layer的解决方案.我做到了

So I have uploaded Python script to AWS lambda. Now I am not sure how to use pysftp library in AWS lambda, so I found solution that add pysftp library Layer in AWS lambda Layer. I did it with

pip3安装pysftp -t ./library_folder

pip3 install pysftp -t ./library_folder

然后压缩上面的文件夹并添加到AWS Lambda层中.

And I make zip of above folder and added in AWS lambda Layer.

但是我仍然遇到很多错误,就像一个接一个:-

But still I got so many errors like one by one :-

没有名为"pysftp"的模块

No module named 'pysftp'

没有名为"paramiko"的模块

No module named 'paramiko'

未定义符号:PyInt_FromLong

Undefined Symbol: PyInt_FromLong

无法从部分初始化的模块"bcrypt"中导入名称"_bcrypt"(很可能是由于循环导入)

cannot import name '_bcrypt' from partially initialized module 'bcrypt' (most likely due to a circular import)

找不到CFFI模块

我只是淡出了上面的错误,所以没有找到合适的解决方案.如何在我的AWS Lambda中无缝使用pysftp库?

I just fade up of above errors I didn't find the proper solution. How can I can use pysftp library in my AWS lambda seamlessly?

推荐答案

我构建了pysftp层,并使用python 3.8在我的lambda上对其进行了测试.只看导入和基本打印:

I build pysftp layer and tested it on my lambda with python 3.8. Just to see import and basic print:

import json
import pysftp

def lambda_handler(event, context):
    # TODO implement
    print(dir(pysftp))
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

我使用以下docker工具构建了pysftp层:

I used the following docker tool to build the pysftp layer:

所以我为pysftp做的是:

# create pysftp fresh python 3.8 environment
python -m venv pysftp

# activate it
source pysftp/bin/activate

cd pysftp

# install pysftp in the environemnt
pip3 install pysftp  

# generate requirements.txt
pip freeze > requirements.txt

# use docker to construct the layer
docker run --rm -v `pwd`:/var/task:z lambci/lambda:build-python3.8 python3.8 -m pip --isolated install -t ./mylayer -r requirements.txt

zip -r pysftp-layer.zip .

其余的将zip文件上传到s3中,在AWS控制台中创建新层,将Compatible runtime设置为python 3.8并在我的测试lambda函数中使用它.

And the rest is uploading the zip into s3, creating new layer in AWS console, setting Compatible runtime to python 3.8 and using it in my test lambda function.

您还可以在此处查看如何使用此docker工具(我使用的docker命令基于其中的内容该链接).

You can also check here how to use this docker tool (the docker command I used is based on what is in that link).

希望这会有所帮助

这篇关于pysftp库在AWS Lambda层中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆