Hello @Vaibhav Patil
To deploy a Python script in Azure Function Service, you can follow these steps:
- Open Visual Studio Code and install the Azure Functions extension.
- Create a new Azure Functions project by clicking on the Azure icon in the left-hand menu, then selecting "Create New Project".
- Choose a folder to store your project in and select "Python" as the language.
- Select "HTTP trigger" as the template for your function.
- Give your function a name and select "Anonymous" for the Authorization level.
- 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)
- Replace
mycontainer
andmyblob
with the name of your container and blob respectively. - 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".
- 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.