pyodbc 找不到 Azure 函数 linux

问题描述 投票:0回答:1

我正在从 azure devops 管道部署 python 函数,我没有门户访问权限,也没有 ssh 访问 azure function linux worker。 azure 函数使用托管标识连接到 SQL 数据库。我收到上述错误。

'''Connect to Azure SQL DB'''
credential = DefaultAzureCredential() # system-assigned identity
# Get token for Azure SQL Database and convert to UTF-16-LE for SQL Server driver
token = credential.get_token("https://database.windows.net/.default").token.encode("UTF-16-LE")
token_struct = struct.pack(f'<I{len(token)}s', len(token), token)
# Connect with the token            
SQL_COPT_SS_ACCESS_TOKEN = 1256
conn_string = f"Driver={{ODBC Driver 17 for SQL Server}};SERVER=sql-to-dev-qasecgate.database.windows.net;DATABASE=QASecurityGatepoc"
database_conn = pyodbc.connect(conn_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})            
database_cursor = database_conn.cursor()
return database_conn, database_cursor

这个 github 线程已将问题标记为已关闭,但显然没有解决。一位用户提到使用

pypyodbc
而不是
pyodbc
。我试过了,但是我无法使用它对 Azure SQL DB 进行身份验证。我找不到使用
pypyodbc
.

为上述场景执行此操作的代码

我不能使用docker。

我记得在早些时候处理这个问题时遇到过同样的问题,但那时我有 ssh 访问权限。我记得手动下载 ODBC 驱动程序并复制文件,可能是

.ini
文件?到某个目录以使其工作。 我现在不记得这些信息,也不确定如何通过管道任务(不是 YAML)完成这些信息。

python-3.x linux azure-functions pyodbc
1个回答
0
投票

如果您想使用

pypyodbc
使用 Managed Identity 从 Azure python 函数应用程序连接到 azure SQL DB,这是代码。

import pypyodbc

server = '<your_server_name>.database.windows.net'
database = '<your_database_name>'
driver = '{ODBC Driver 17 for SQL Server}'
auth = 'ActiveDirectoryMsi'

connection_string = (
    'DRIVER=' + driver + ';'
    'Server=' + server + ';'
    'Database=' + database + ';'
    'Authentication=' + auth + ';'
)

cnxn = pypyodbc.connect(connection_string)

print("Connected")
© www.soinside.com 2019 - 2024. All rights reserved.