Last active
January 6, 2026 18:11
-
-
Save ruddra/4b4066cb901587ae51c0a78ebd7b19f5 to your computer and use it in GitHub Desktop.
Azure Read Blob Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Add the following function to your function_app.py | |
| import azure.functions as func | |
| import logging | |
| import os | |
| from azure.identity import DefaultAzureCredential | |
| from azure.storage.blob import BlobServiceClient | |
| @app.route(route="readblob", auth_level=func.AuthLevel.ANONYMOUS) | |
| def http_read_blob(req: func.HttpRequest) -> func.HttpResponse: | |
| logging.info('Python HTTP trigger processing a request.') | |
| # 1. Get the account URL (e.g., https://aaa.blob.core.windows.net) | |
| # Best practice: Store this in an App Setting | |
| account_url = os.environ.get("BLOB_STORAGE_ACCOUNT_URL") | |
| container_name = "mycontainer" | |
| blob_name = req.params.get('file') | |
| if not blob_name: | |
| return func.HttpResponse("Please pass a 'file' name in the query string", status_code=400) | |
| try: | |
| # 2. Setup Identity and Client | |
| # This will use the Managed Identity in Azure | |
| token_credential = DefaultAzureCredential() | |
| blob_service_client = BlobServiceClient( | |
| account_url, | |
| credential=token_credential | |
| ) | |
| # 3. Read the blob data | |
| blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) | |
| blob_data = blob_client.download_blob().readall() | |
| return func.HttpResponse(blob_data, mimetype="application/octet-stream") | |
| except Exception as e: | |
| logging.error(f"Error reading blob: {e}") | |
| return func.HttpResponse(f"Failed to read blob: {str(e)}", status_code=500) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| azure-storage | |
| azure-identity |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment