DEV Community

Cover image for How to use Azure Blob Storage with Python
Nelson Hernández
Nelson Hernández

Posted on • Edited on

4 3

How to use Azure Blob Storage with Python

In this publication we will see the main methods to use with Azure Blob Storage

pip install azure-storage-blob
Enter fullscreen mode Exit fullscreen mode

Blob Service Client

from os import getenv
from azure.storage.blob import BlobServiceClient

blob_service_client = BlobServiceClient.from_connection_string(
    getenv("AZURE_STORAGE_CONNECTION_STRING"))
Enter fullscreen mode Exit fullscreen mode

Connection String

Connection String Azure Blob Storage

Methods for blobs (Files)

Upload Blob

def upload_blob(filename: str, container: str, data: BinaryIO):
    try:
        blob_client = blob_service_client.get_blob_client(
            container=container, blob=filename)

        blob_client.upload_blob(data)

        print("success")
    except Exception as e:
        print(e.message)
Enter fullscreen mode Exit fullscreen mode

Download Blob

def download_blob(filename: str, container: str):
    try:
        blob_client = blob_service_client.get_blob_client(
            container=container, blob=filename)

        print(blob_client.download_blob().readall())
    except Exception as e:
        print(e.message)
Enter fullscreen mode Exit fullscreen mode

Delete Blob

def delete_blob(filename: str, container: str):
    try:
        blob_client = blob_service_client.get_blob_client(
            container=container, blob=filename)

        blob_client.delete_blob()

        print("success")
    except Exception as e:
        print(e.message)
Enter fullscreen mode Exit fullscreen mode

Methods for Containers (Folders)

Create Container

def create_container(container: str):
    try:
        blob_service_client.create_container(container)
        print("success")
    except Exception as e:
        print(e.message)
Enter fullscreen mode Exit fullscreen mode

Delete Container

def delete_container(container: str):
    try:
        blob_service_client.delete_container(container)
        print("success")
    except Exception as e:
        print(e.message)
Enter fullscreen mode Exit fullscreen mode

List Containers

def get_containers():
    try:
        containers = blob_service_client.list_containers()
        print([container.name for container in containers])
    except Exception as e:
        print(e.message)
Enter fullscreen mode Exit fullscreen mode

Github Gist

https://gist.github.com/nelsoncode019/eade5071c80f659bfa7ce9a452345d85

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay