在 httptrigger 中获取 Keyvault Secret 并使用它来获取要由 Function-Python 输出的信息
Acquire Keyvault Secret within a httptrigger and Use it to Acquire Info to be output by Function-Python
我有以下代码,我用它来获取秘密,使用秘密登录门户并下载 csv table。这在函数外工作正常。
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/table,以便 table 作为 json 主体返回触发响应或存储到 blob 中。我怎样才能做到这一点?
我最初认为我可以通过在这个 中将 vault 集成到 http 触发器中来实现这一点。但是,我最终返回的是 Secret,而我一直无法在函数中使用该 Secret。
我不介意,即使是登录电子邮件帐户或任何其他门户的示例也足够了,前提是在函数运行时内获取了秘密密码。最后,我有兴趣了解如何检索秘密并在函数中使用它以 power/resource 函数输出。
代码是我在本地用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):
我有以下代码,我用它来获取秘密,使用秘密登录门户并下载 csv table。这在函数外工作正常。
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/table,以便 table 作为 json 主体返回触发响应或存储到 blob 中。我怎样才能做到这一点?
我最初认为我可以通过在这个
我不介意,即使是登录电子邮件帐户或任何其他门户的示例也足够了,前提是在函数运行时内获取了秘密密码。最后,我有兴趣了解如何检索秘密并在函数中使用它以 power/resource 函数输出。
代码是我在本地用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):