python-3.x - 在 httptrigger 中获取 Keyvault Secret 并使用它来获取要由 Function-Python 输出的信息

标签 python-3.x azure-functions azure-keyvault

我有以下代码,我用它来获取 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): enter image description here

关于python-3.x - 在 httptrigger 中获取 Keyvault Secret 并使用它来获取要由 Function-Python 输出的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65119379/

相关文章:

Azure 函数 key 保管库

python - urllib.request.urlopen:SSL:Windows> = Vista(7/8/10/Server 2008)在Windows> = 3.4上的Windows上的SSL:CERTIFICATE_VERIFY_FAILED错误

python - 如何转换包含单个字符串且字符串内有多个条目的列表。 python 3

python - 为什么并行读取大型 CSV 时节省的时间很少?

azure - Azure 函数中的 Cosmos DB 408 响应

java - 使用 Key Vault 中的 key 连接到 Cosmos

azure - 在 aspnetcore 应用程序中共享开发 secret 的最简单方法

python - 防止 sleep 模式python(python上的Wakelock)

azure - log.Info 和 log.Verbose - 在 Azure 门户中哪里可以看到输出?

Azure Functions "Failed"指标上的 Azure 警报正在触发,没有明显的故障