![](/img/trans.png)
[英]unable to get the secret / certificate from Azure Key Vault using python | 'KeyVaultManagementClient' object has no attribute 'get_secret'
[英]Unable to connect Azure SQL database from Python using Key Vault Secret
我的密鑰保管庫秘密是
integrated security=False;encrypt=True;connection timeout=30;data source=yyy.database.windows.net;initial catalog=db-xxxx;user id=xx-user;password=pwd-xx
我能夠使用來自 Azure ADF 的上述 KV 機密連接到 Azure SQL 數據庫。 我正在嘗試通過 Python 代碼做同樣的事情:
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
import pyodbc
KVUri = "https://yyy-kv.vault.azure.net/"
credential = DefaultAzureCredential()
client = SecretClient(vault_url=KVUri, credential=credential)
secretName = "xxxx"
print("Retrieving your secret")
retrieved_secret = client.get_secret(secretName)
print(f"Your secret is '{retrieved_secret.value}'.")
print(" done.")
# The code fails after this.
with pyodbc.connect(retrieved_secret.value) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 3 name, collation_name FROM sys.databases")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
但是代碼失敗並出現以下錯誤:
Traceback (most recent call last):
File "first.py", line 25, in <module>
with pyodbc.connect(retrieved_secret.value) as conn:
pyodbc.InterfaceError: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
您能幫我在 KV 中進行哪些更改,還是 Azure 不支持 Python KV? 謝謝。
您的連接字符串應如下所示。
Driver={ODBC Driver 17 for SQL Server};Server=yy.database.windows.net,1433;Database=dbname;Uid=sasasa;Pwd={pwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;
我相信如果你改變 kv 中的秘密,你可以解決這個問題。
官方文檔:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.