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

1 Answer 1

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
        )

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for this. it working. Quick question. The Function app will always restart once the file is update in the storage account right?
@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.
Thanks for the reply. Is there a way I can configure the Function app to restart itself or have something externally restart it.
@Jaychuks Does this command meet your requirement ?
@hanshih Sorry, I do not know much about what you concern.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.