Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data from Azure blob storage in Azure Function in python

Please how do I read in data from my Azure Storage account when I launch my Function app. I need to read the saved weights for my machine learning model at runtime. I want to read the model directly from the storage account because the model is expected to be updated daily and do not want have to manually redeploy the model.

Thanks

like image 988
Jay chuks Avatar asked Mar 02 '23 22:03

Jay chuks


1 Answers

For this requirement, you can go to your storage blob first and click "Generate SAS" to generate "Blob SAS URL" (you can also define the start date and expiry date of the url). enter image description here

Then go to your python function, install azure-storage-blob module by running pip install azure-storage-blob command in VS code. After that, write the function code like: enter image description here

Start the function and trigger it, we can see the content of test1.txt printed out by logging.info. enter image description here

Below is all of my function code for your reference:

import logging

import azure.functions as func

from azure.storage.blob import BlobClient


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    blob_client = BlobClient.from_blob_url("copy your Blob SAS URL here")
    download_stream = blob_client.download_blob()
    logging.info('=========below is content of test1')
    logging.info(download_stream.readall())
    logging.info('=========above is content of test1')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

like image 147
Hury Shen Avatar answered Mar 05 '23 00:03

Hury Shen