Python 3.x 获取httptrigger中的Keyvault Secret,并使用它获取要由函数Python输出的信息

Python 3.x 获取httptrigger中的Keyvault Secret,并使用它获取要由函数Python输出的信息,python-3.x,azure-functions,azure-keyvault,Python 3.x,Azure Functions,Azure Keyvault,我有下面的代码,我用它来获取一个秘密,使用秘密登录到门户网站并下载一个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 = DefaultAzureCredenti

我有下面的代码,我用它来获取一个秘密,使用秘密登录到门户网站并下载一个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/table,以便将该表作为触发器响应的json主体返回或存储到blob中。我怎样才能做到这一点

我最初认为可以通过将vault集成到http触发器中来实现这一点。但是,我最终返回了这个秘密,我无法在函数中使用这个秘密

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 

我不介意,只要在函数运行时内获得了密码,即使是登录到电子邮件帐户或任何其他门户的示例也足够了。最后,我感兴趣的是了解如何检索一个秘密,并在函数中使用它来为函数输出提供电源/资源。

代码是我在本地使用csv文件测试的内容。但是我不确定这行代码是否在你这边起作用。如果代码显示错误,您可以自己进行一些测试和修改

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机密):

那么,代码最后一行的
表格
是csv对象吗?@Hury Shen是的,表格是csv对象。如果需要,可以转换为其他格式,因此如果csv构成challenge@Hury即使csv被抛成一团,沈也不会介意。这里的主要内容是在函数中检索和使用vault secret。您还没有解决如何在函数中获取密钥vault secret的问题吗?根据我对这篇文章的理解,你想解决的问题是如何从函数中响应json(或将json存储在blob中),对吗?好的,明白。@wwnde是的,有什么我能帮忙的吗?@wwnde我不关注数据仓库,你可以发布关于堆栈溢出的问题,如果我能提供帮助,我会尽力提供建议。
https://stackoverflow.com/questions/65573962/azure-webhooks-python
,实际上,这一切都是关于创建一个能够监听特定url的webhook。这在Azure中是如何实现的?似乎找不到合适的文件