Processing text files via Azure functions

Viewed 183

I'm new to Azure Functions and I want to deploy a python script I wrote as a webservice. The script takes a text file and outputs the language the file is written in as a json. Should I be using a Http request and how do I modify my script so that it accepts a text file via a put request?

1 Answers

One of the workaround is to connect to your storage account and read the .txt file using blob trigger

SCENARIO - 1 Reading directly from blob storage

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobClient, BlobServiceClient

# Connecting to your account
account_url = "https://storage1.blob.core.windows.net/"

creds = DefaultAzureCredential()
service_client = BlobServiceClient(
    account_url=account_url,
    credential=creds
)

# Reading the blob
blob_download = blob_client.download_blob()
blob_content = blob_download.readall().decode("utf-8")
print(f"Your content is: '{blob_content}'")

SCENARIO - 2 Downloading the .txt file and then processing

  def download_file(self, source, dest):
    # Download a single file to a path on the local filesystem 
    # dest is a directory if ending with '/' or '.', otherwise it's a file
    if dest.endswith('.'):
      dest += '/'
    blob_dest = dest + os.path.basename(source) if dest.endswith('/') else dest

    print(f'Downloading {source} to {blob_dest}')
    os.makedirs(os.path.dirname(blob_dest), exist_ok=True)
    bc = self.client.get_blob_client(blob=source)
    with open(blob_dest, 'wb') as file:
      data = bc.download_blob()
      file.write(data.readall())

and then here is the code to convert text file to JSON

import json  
  
# the file to be converted
filename = '<YOUR .TXT FILE PATH>'
  
# resultant dictionary
dict1 = {}
  
# fields in the sample file 
fields =['name', 'designation', 'age', 'salary']
  
with open(filename) as fh:     
      
    # count variable for employee id creation
    l = 1
      
    for line in fh:
          
        # reading line by line from the text file
        description = list( line.strip().split(None, 4))
          
        # for output see below
        print(description) 
          
        # for automatic creation of id for each employee
        sno ='emp'+str(l)
      
        # loop variable
        i = 0
        # intermediate dictionary
        dict2 = {}
        while i<len(fields):
              
                # creating dictionary for each employee
                dict2[fields[i]]= description[i]
                i = i + 1
                  
        # appending the record of each employee to
        # the main dictionary
        dict1[sno]= dict2
        l = l + 1
  
  
# creating json file        
out_file = open("test2.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close()

For instance here is my text file

enter image description here

Here is the JSON file that I received

enter image description here

REFERENCE:

  1. Convert Text file to JSON in Python
  2. Python program to download a complete directory or file
  3. Reading and Writing an Azure Storage Blob from Python
Related