我正在尝试使用 AWS Lambda 连接到 AWS RDS。我将 PyMySQL 安装到目录并使用下面的代码和库构建包
import sys
import pymysql
def lambda_handler(event, context):
string=""
try:
connection = pymysql.connect(host='',
user='',
password='',
db='',
charset='',
cursorclass=pymysql.cursors.DictCursor)
cur = connection.cursor(pymysql.cursors.DictCursor)
cur.execute("select * from table")
for row in cur:
print(row['col'])
string+=row['col']
except Exception as e:
print("MySQL error: %s" % (e.args[0]))
return string
print(lambda_handler("",""))
在我的机器上,上面的代码有效,但在 AWS 中,它显示
MySQL error: module 'pymysql' has no attribute 'connect'
我查看了pymysql只在有代码的目录下可用,所以不知道为什么不能使用connect方法。 两个 Python 版本是相同的。
编辑:
Traceback (most recent call last):
File "/var/task/lambda.py", line 7, in lambda_handler
connection = pymysql.connect(host='',
AttributeError: module 'pymysql' has no attribute 'connect'
请您参考如下方法:
试试 zip -r package.zip *
我怀疑您只压缩了 pymysql 模块的顶层,而不是其子目录的内容
用于上传到 lambda 的 AWS 文档非常糟糕。
