Azure Function using v2 programming model for debugging

142 Views Asked by At

I have inherited the following code in Azure Functions and I want to ensure if this correct and find out the correct way to call this utilizing Python Azure Functions v2 programming model.

import os
import gnupg
import azure.functions as func

gpg = gnupg.GPG(gnupghome='/tmp')

class PGPDecryption:
def __init__(self, prikey):
    self.fullkey = prikey

def decrypt(self, blob_data):
    key_data = self.fullkey.strip()
    gpg.import_keys(key_data)
    decrypted_data = gpg.decrypt(blob_data)
    return str(decrypted_data)

# Blob Trigger for input and Blob Output binding for decrypted file
@func.blob_input(name='inputBlob', path='input-container/{name}', 
connection='AzureWebJobsStorage')
@func.blob_output(name='outputBlob', path='output-container/{name}.decrypted', 
connection='AzureWebJobsStorage')
def main(inputBlob: func.InputStream, outputBlob: func.Out[func.InputStream], context: 
func.Context):
prikey = '<Your PGP Private Key>'  # Replace with your PGP private key
decryptor = PGPDecryption(prikey)
decrypted_data = decryptor.decrypt(inputBlob.read())

# Writing decrypted data to output binding
outputBlob.set(decrypted_data)   

The objective is for the code to get a file from Azure Blob and decrpyt it and put it back to another Azure Blob location. Does the code actually achieve this and how can I debug this?

1

There are 1 best solutions below

0
RithwikBojja On

Does the code actually achieve this and how can I debug this?

To debug you can use logging and to test the code(taken a sample code, you can use yours):

import azure.functions as func
import logging

app = func.FunctionApp()

@app.blob_trigger(arg_name="myblob", path="rithwiktest1",
                               connection="rithwiktest1")
@app.blob_output(arg_name='outputBlob', path='testrithwik2/test',
                 connection='rithwiktest1') 
def blob_trigger(myblob: func.InputStream,outputBlob: func.Out[str]):
    logging.info(f"Python blob trigger function processed blob"
                f"Name: {myblob.name}"
                f"Blob Size: {myblob.length} bytes")
    outputBlob.set("Hello Rithwik Bojja")  

Here, this azure blob trigger gets triggered when a new blob is added and then it created a new file in destination like below:

enter image description here

You need to check if a new blob is created or not.