how to fix http_trigger function failing

Raj Deep 0 Reputation points
2025-02-14T15:39:07.6166667+00:00

Hi ,

I am decrypting tar file and extracting to blob version by calling python code in azure function

code is working fine in vs studio local

but it shows failure without any reason in azure functions portal and also when called from adf pipeline.

I was unable to track back anything . help required

import io
import os
import logging
import gnupg
import tarfile
import azure.functions as func
from azure.storage.blob import BlobServiceClient
# Constants
CONTAINER_NAME = ""
BLOB_NAME = ""
OUTPUT_FOLDER = ""
GPG_PASSPHRASE = "*"
# Blob Storage Connection
BLOB_CONNECTION_STRING = ""
# Initialize Blob Service Clients
blob_service_client = BlobServiceClient.from_connection_string(BLOB_CONNECTION_STRING)
container_client = blob_service_client.get_container_client(CONTAINER_NAME)
app = func.FunctionApp()
@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Processing HTTP trigger function.')
    try:
        main()
        return func.HttpResponse("Hello, World! Decryption and extraction completed.", status_code=200)
    except Exception as e:
        logging.error(f"Function failed: {str(e)}")
        return func.HttpResponse(f"Error: {str(e)}", status_code=500)
# GPG Decryption Function
def decrypt_gpg_from_blob(blob_name: str, passphrase: str) -> io.BytesIO:
    logging.info(f"Downloading and decrypting {blob_name} from Azure Blob Storage...")
    try:
        # Get the blob client
        blob_client = container_client.get_blob_client(blob_name)
        encrypted_data = blob_client.download_blob().readall()
        # Initialize GPG
        gpg = gnupg.GPG()
        
        # Perform decryption
        decrypted_data = gpg.decrypt(encrypted_data, passphrase=passphrase)
        if not decrypted_data.ok:
            logging.error(f"Decryption failed: {decrypted_data.stderr}")
            raise ValueError(f"GPG decryption failed: {decrypted_data.stderr}")
        logging.info("Decryption successful.")
        return io.BytesIO(decrypted_data.data.encode())  # Ensure proper encoding
    except Exception as e:
        logging.error(f"Error during GPG decryption: {str(e)}")
        raise
# Extract & Upload Files to Azure Blob
def extract_and_upload_to_blob(decrypted_data: io.BytesIO, output_folder: str):
    logging.info(f"Extracting and uploading files to {output_folder} in Azure Blob Storage...")
    try:
        output_container_client = blob_service_client.get_container_client(output_folder)
        # Ensure the output container exists
        if not output_container_client.exists():
            output_container_client.create_container()
        with tarfile.open(fileobj=decrypted_data, mode='r:*') as tar:
            for member in tar.getmembers():
                if member.isfile():
                    file_stream = tar.extractfile(member)
                    if file_stream:
                        file_data = file_stream.read()
                        cleaned_path = os.path.basename(member.name)
                        upload_blob_with_debug(output_container_client, cleaned_path, file_data)
                        logging.info(f"Uploaded {cleaned_path} to {output_folder}.")
    except tarfile.TarError as e:
        logging.error(f"Error extracting tar file: {str(e)}")
        raise
    except Exception as e:
        logging.error(f"Error during file extraction or upload: {str(e)}")
        raise
# Upload to Azure Blob with Debug Logging
def upload_blob_with_debug(blob_container_client, blob_name, data):
    try:
        blob_client = blob_container_client.get_blob_client(blob_name)
        logging.info(f"Uploading blob: {blob_name} to {blob_client.url}")
        blob_client.upload_blob(data, blob_type="BlockBlob", overwrite=True)
        logging.info("Blob uploaded successfully.")
    except Exception as e:
        logging.error(f"Error uploading blob: {e}")
# Main Function
def main():
    logging.basicConfig(level=logging.INFO)
    try:
        decrypted_data = decrypt_gpg_from_blob(BLOB_NAME, GPG_PASSPHRASE)
        extract_and_upload_to_blob(decrypted_data, OUTPUT_FOLDER)
        logging.info("Decryption and extraction completed successfully.")
    except Exception as e:
        logging.error(f"An error occurred: {str(e)}")

i am seeing issue as below

Screenshot 2025-02-14 151755

Any help is greatly appreciated

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,441 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 5,575 Reputation points Microsoft Employee
    2025-02-17T10:55:03.6466667+00:00

    Hello @Raj Deep

    To troubleshoot this issue, you can follow these steps:

    1. Enable Detailed Logging: You can add more detailed logging statements in your code to capture additional information that might help identify the problem. For example, you can log the intermediate steps, such as the result of decryption, extraction, and uploading.
    2. Check Azure Function Logs: You can check the logs in the Azure Functions portal to see if there are any error messages or exceptions logged when the function fails. This can provide more insights into what went wrong.
    3. Handle Exceptions: Make sure to handle exceptions properly in your code. It's important to catch exceptions and log detailed error messages to understand where the problem is occurring.
    4. Verify Environment Configuration: Double-check the configuration settings, such as the connection strings, container names, and blob names, to ensure they are correctly set up in the Azure Function environment.
    5. Test Locally: If possible, try running the function locally in Visual Studio Code to see if you can reproduce the issue outside of the Azure Functions environment. This can help isolate whether the problem is specific to the Azure Functions setup. By following these steps and analyzing the logs, you should be able to identify the root cause of the HTTP trigger function failure in your Azure Function.

    If you encounter any specific errors or exceptions during the troubleshooting process, feel free to share them for further assistance.

    I hope this helps.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.