首页 > 解决方案 > 使用 Python 38 从 AWS Lambda 连接 OracleDB 的问题

问题描述

我正在尝试使用 cx_Oracle 库使用 python 访问 lambda 中的 oracle db。由于 oracle 需要 os 特定的客户端来连接,我还下载了即时客户端并将其添加为 AWS 中的一个层。问题是我看到这个不会让步的错误

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory"

lambda_handler.py

import cx_Oracle
import os


def lambda_handler(event, context):

    dsn = cx_Oracle.makedsn('url', 1521)
    con = cx_Oracle.connect(user='user', password='1234', dsn=dsn)
    cursor = con.cursor()
    cursor.execute('SELECT * FROM DEVICES')
    
    return {
        'statusCode': 200,
        'body': cursor.fetchall()
    }

我已经看到其他线程添加了 init_oracle_client,所以我添加了cx_Oracle.init_oracle_client(lib_dir='/opt/instantclient_21_1') 因为 Lambda 层位于“/opt”目录中,它似乎找到了该文件,但它产生了类似的错误。注意文件不同

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libnnz21.so: cannot open shared object file: No such file or directory"

据我了解,由于我专门放置了 lib_dir,因此该文件应该是“/opt/instantclient_21_1/libnnz21.so”,但由于某种原因,Oracle 尝试在当前运行时目录中找到该库。

注意我正在使用:

标签: pythonoracleaws-lambdacx-oracleinstantclient

解决方案


推荐阅读