7

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

Jay chuks
  • 389
  • 1
  • 5
  • 18

1 Answers1

10

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
        )

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Thanks for this. it working. Quick question. The Function app will always restart once the file is update in the storage account right? – Jay chuks Nov 12 '20 at 19:46
  • @Jaychuks Sorry, I don't understand what do you mean. I don't think function app will restart once a file is update in storage. – Hury Shen Nov 13 '20 at 01:49
  • Thanks for the reply. Is there a way I can configure the Function app to restart itself or have something externally restart it. – Jay chuks Nov 13 '20 at 05:21
  • @Jaychuks Does this [command](https://learn.microsoft.com/en-us/powershell/module/az.functions/restart-azfunctionapp?view=azps-5.0.0) meet your requirement ? – Hury Shen Nov 13 '20 at 05:26
  • Yes is exactly what I need. is there a way I can run this command on an ADF pipeline? or automate running it every day at a particular time? – Jay chuks Nov 16 '20 at 23:23
  • Hi @Jaychuks I'm don't know much about ADF. But for this requirement, you can create an automation account in your azure and create a runbook to run the restart powershell command. And also need to create a schedule for it. Apart from use automation account, you can also use azure function timer trigger to run the powershell command at particular time. – Hury Shen Nov 17 '20 at 02:15
  • Do you know how to disable the verbose of azure storage module? The HTTP messages are really annoying. – han shih Apr 12 '21 at 08:20
  • 1
    @hanshih Sorry, I do not know much about what you concern. – Hury Shen Apr 12 '21 at 08:22
  • Do we always need to download first ? blob_client.download_blob() – Sarde Jun 28 '21 at 11:14