简体   繁体   中英

Json to excel from devops to storage account with help of azure function in python

There is a.json file in the devops repository. What we need to do is to make an azure function which will take the.json file from azure devops repository, then convert it into excel, and at last saved it into azure storage account as blob.

My approach -

  1. I am creating a http trigger and call an REST api call to fetch the data from the devops repo with use of headers and the pattern url.

I have added few headers in the code like:-

pat = 'access-token'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
logging.info("----------------auth--------------")
headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}
logging.info('*************headers************')
response = requests.get(
        url="https://dev.azure.com/{{organisation}}/?{{repository}}&commitOrBranch=master&api-version=5.0-preview.1&path={{path}}.json", headers=headers)
logging.info('--------------response-------------')
logging.info(response)

But this is not giving me the content in the response.

and giving logs like:-

2022-01-28T11:26:04Z   [Information]   ----------------auth--------------
2022-01-28T11:26:04Z   [Information]   *************headers************
2022-01-28T11:26:05Z   [Information]   --------------response-------------

skipping the result..

  1. The second part is to convert this into excel, I have code to do that and it is working fine with local path.

  2. Third step is to store that excel in storage account.

I am stuck because I am not getting any content in the response and as I am new to python, I don't know how to load that json.

and the third step, how to save the result into storage account.

Thanks for giving any response in advance and you can ask if you need any details.

I have edit the question for more details.

I am creating a http trigger and call an REST api call to fetch the data from the devops repo with use of headers and the pattern url.

As per my work around below is the sample code which will help you in downloading the data from Azure Repos in JSON format. As.you told that you are not getting any response may be because PAT token issue

import requests
import base64

pat = 'PAT'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}

response = requests.get(
    url="https://vssps.dev.azure.com/{Organization}/_apis/graph/users?api-version=6.0-preview.1", headers=headers)
print(response)

The second part is to convert this into excel, I have code to do that and it is working fine with local path.

You can use pandas and openpyxl for converting the JSON file into.XLSX file and here is the sample code for that.

import pandas
pandas.read_json("input.json").to_excel("output.xlsx")

Third step is to store that excel in storage account.

Here is the Documentation for uploading files into storage account using python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM