我有以下代码,我用它来获取 secret ,使用 secret 登录门户并下载 csv 表。这在函数外工作正常。
import pandas as pd
import pandas as pd
from arcgis.gis import GIS
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url="https://xxxx-dev-vault.vault.azure.net/", credential=credential)
secret = secret_client.get_secret("Secret")
#Log into portal
gis = GIS("https://url.com", "Username", secret.value)
#Extracting Table
item=gis.content.get('content_id')
table=item.layers[0].query().sdf
我需要在我的 httptrigger 中包含这段代码,以便该函数登录门户,提取 csv/表,以便该表作为触发器响应的 json 主体返回或存储到 blob 中。我怎样才能做到这一点?
我最初认为我可以通过将保险库集成到这个 post 的 http 触发器中来实现这一点.但是,我最终返回的是 Secret,而我一直无法在函数中使用该 Secret。
我不介意,即使是登录电子邮件帐户或任何其他门户的示例也足够了,前提是在函数运行时内获取了 secret 密码。最终,我有兴趣了解如何检索 secret 并在函数中使用它来为函数输出提供动力/资源。
最佳答案
代码是我在本地用csv文件测试的。但我不确定 dict_reader = csv.DictReader(table)
行是否适合您。如果报错可以自行测试修改代码。
import logging
import azure.functions as func
from arcgis.gis import GIS
import csv
import json
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# do some configuration in application settings of your function app as previous post mentioned, and then we can get the secret of key vault.
# secret = os.environ["testkeyvault"]
# gis = GIS("https://url.com", "Username", secret)
#Extracting Table
# item=gis.content.get('content_id')
# table=item.layers[0].query().sdf
# The four lines below is what I test in my side, I use a csv file in local and convert it to json and use "return func.HttpResponse(json_from_csv)" at the end of the code. The function will response json.
file = open("C:\\Users\\Administrator\\Desktop\\user.csv", "r")
dict_reader = csv.DictReader(file)
dict_from_csv = list(dict_reader)[0]
json_from_csv = json.dumps(dict_from_csv)
# Your code should be like the three lines below. But as I didn't test with the csv file from gis, so I'm not sure if "dict_reader = csv.DictReader(table)" can work.
# dict_reader = csv.DictReader(table)
# dict_from_csv = list(dict_reader)[0]
# json_from_csv = json.dumps(dict_from_csv)
return func.HttpResponse(json_from_csv)
=============================更新========== ==================
更改代码以符合 OP 的要求(并且不要忘记将函数部署到 azure,否则我们无法在 azure 上获取 keyvault secret):
关于python-3.x - 在 httptrigger 中获取 Keyvault Secret 并使用它来获取要由 Function-Python 输出的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65119379/