在httptrigger中获取Keyvault Secret,并使用它获取由Function-Python输出的信息 [英] Acquire Keyvault Secret within a httptrigger and Use it to Acquire Info to be output by Function-Python

查看:36
本文介绍了在httptrigger中获取Keyvault Secret,并使用它获取由Function-Python输出的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,用于获取机密,使用机密登录门户网站并下载csv表.在函数外部可以正常工作.

 将pandas导入为pd将熊猫作为pd导入从arcgis.gis导入GIS从azure.identity导入DefaultAzureCredential从azure.keyvault.secrets导入SecretClient凭证= DefaultAzureCredential()secret_client = SecretClient(vault_url ="https://xxxx-dev-vault.vault.azure.net/",凭据=凭据)秘密= secret_client.get_secret(秘密")#登录门户gis = GIS("https://url.com",用户名",secret.value)#提取表item = gis.content.get('content_id')table = item.layers [0] .query().sdf 

我需要在我的httptrigger中包括这部分代码,以便函数登录到门户网站,提取csv/table,以便将该表作为触发器响应的json主体返回或存储到blob中.我该如何实现?

我最初以为我可以通过在

I have the following code which I use to acquire a secret, use secret to log into portal and download a csv table. This works ok outside a function.

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 

I need to include this bit of code in in my httptrigger so that the function logs into portal, extracts csv/table so that the table is returned as a json body of the trigger response or is stored into a blob. How can I achieve this?

I initially thought I could achieve this by integrating the vault in the http trigger in this post. However, I ended up with the Secret being returned instead and I have been unable to use the secret within the function.

I dont mind, even an example logging into an email account or any other portal will suffice provided the secret password is acquired within the function runtime. Ultimately, I am interested in understanding how to retrieve a secret and use it within a function to power/resource a function output.

解决方案

The code is what I test in my side with a csv file in local. But I'm not sure if the line dict_reader = csv.DictReader(table) works in your side. You can do some test and modify the code by yourself if it show error.

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)

=============================Update============================

Change the code to match OP's requirements(And do not forget deploy the function to azure, or we can't get the keyvault secret on azure):

这篇关于在httptrigger中获取Keyvault Secret,并使用它获取由Function-Python输出的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆