I want to run and deploy a Python script in Azure Function Service. The script is supposed to store a file in Azure Blob Storage, but the file is not being uploaded to Blob Storage using Azure Functions and VS Code. Please provide the steps to resolve thi

Vaibhav Patil 235 Reputation points
2024-08-06T12:08:22.47+00:00

I'm trying the following approach and receiving a 200 response, but the file is not being stored in Blob Storage. The same code runs correctly in the Spyder IDE. Please provide a solution for this issue.

I want to run and deploy a Python script in Azure Function Service. The script is supposed to store a file in Azure Blob Storage, but the file is not being uploaded to Blob Storage using Azure Functions and VS Code. Please provide the steps to resolve this issue.

User's image

Thanks in advance for any assistance.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,895 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,789 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
10,569 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 3,905 Reputation points Microsoft Employee
    2024-08-09T09:00:47.7466667+00:00

    Hello @Vaibhav Patil

    To deploy a Python script in Azure Function Service, you can follow these steps:

    1. Open Visual Studio Code and install the Azure Functions extension.
    2. Create a new Azure Functions project by clicking on the Azure icon in the left-hand menu, then selecting "Create New Project".
    3. Choose a folder to store your project in and select "Python" as the language.
    4. Select "HTTP trigger" as the template for your function.
    5. Give your function a name and select "Anonymous" for the Authorization level.
    6. In the function code, add the following code to upload a file to Azure Blob Storage:
    import os from azure.storage.blob
    import BlobServiceClient, BlobClient, ContainerClient
    def main(req):
    try: 
    # Get the connection string for the storage account
    connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
    
    # Create a BlobServiceClient 
    object blob_service_client = BlobServiceClient.from_connection_string(connect_str) 
    
    # Create a BlobClient 
    object blob_client = blob_service_client.get_blob_client(container='mycontainer', blob='myblob')
    
    # Upload the file to Blob Storage
    with open('myfile.txt', 'rb') as data: blob_client.upload_blob(data)
    	return 'File uploaded successfully!' 
    
    except Exception as e: 
    	return str(e)
    
    
    1. Replace mycontainer and myblob with the name of your container and blob respectively.
    2. Save the function code and deploy it to Azure Functions by right-clicking on the function in the left-hand menu and selecting "Deploy to Function App".
    3. Once the function is deployed, you can test it by clicking on the "Test" tab in the Azure portal and providing a sample request body.

    If you are still having issues with the file not being uploaded to Blob Storage, please make sure that you have the correct connection string and that the container and blob names are correct. You can also try adding print statements to your code to debug the issue.


    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.

    0 comments No comments

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.