Search code examples
pythonazureazure-storage

Create Azure static web hosting from storage account using Python SDK


I am trying to create the static webhosting for the azure storage account using the python SDK. I am trying to find the examples but not able to find anything.

As per SDK we have to use BlobServiceClient to create static hosting. But i am not able to find any code for this https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-blob


Solution

  • Trying to create static web hosting for the Azure storage account using the Python SDK.

    You can use the code below to create a static website host using the Azure Python SDK.

    Code:

    from azure.storage.blob import BlobServiceClient, StaticWebsite
    
    connection_string = "xxxx"
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    static_website = StaticWebsite(enabled=True, index_document="index.html", error_document404_path="error.html")
    blob_service_client.set_service_properties(static_website=static_website)
    
    # Now you can see the updated properties
    properties = blob_service_client.get_service_properties()
    print(properties['static_website'])
    

    The above code sets up a static website on an Azure Blob Storage account using the StaticWebsite class from the azure.storage.blob module. It then retrieves the updated service properties and prints the static_website property.

    Output:

    {'enabled': True, 'index_document': 'index.html', 'error_document404_path': 'error.html', 'default_index_document_path': None}
    

    enter image description here

    Portal: enter image description here

    After uploading my content to my $web container, I can able to view the content using the URL.

    Browser: enter image description here

    Reference: azure.storage.blob.BlobServiceClient class | Microsoft Learn