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

查看:11
本文介绍了在 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/", credential=credential)秘密 = secret_client.get_secret(秘密")#登录门户gis = GIS("https://url.com", "用户名", secret.value)#提取表item=gis.content.get('content_id')表=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天全站免登陆