Azure Blob
Intro
  • accounts -> containers -> blobs
  • blob, the objects are stored in the key-value pair, key is the name of the stored object, value is the object’s data
  • storage tiers
  • Azure storage account
  • Cheapest per GB storage
  • Azure Storage Explorer
  • Download Azure Storage Explorer
  • Create connection with name and Connection string, which are located in "Access Keys"
  • CLI
  • Installation
  • Login
    az login -u username -p passwd
                    
  • Python
  • Installation
    pip install azure-storage-blob
                
  • Import
    from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
                
  • Authentication
    # obtain connection string from Access Keys of a storage account
    connect_str = <connection string>
                
  • Container
    # create a container
    container_name = 'tempc2'
    blob_service_client = BlobServiceClient.from_connection_string(connect_str) # BlobServiceClient
    
    container_client = blob_service_client.create_container(container_name) # ContainerClient
                
    # list blobs in a container
    container_client = blob_service_client.get_container_client("tempc2") # ContainerClient
    blob_list = container_client.list_blobs()
    for blob in blob_list:
        print("\t" + blob.name)
                
    # delete a container
    container_client = blob_service_client.get_container_client("tempc2")
    container_client.delete_container()
                
  • Blob
    # upload a blob to container
    # Create a blob client using the local file name as the name for the blob
    blob_client = blob_service_client.get_blob_client(container='tempc2', blob='temp.csv') # BlobClient
    
    with open('temp.csv', "rb") as data:
        blob_client.upload_blob(data)
                
    # download a blobs
    # Create a blob client using the blob name in the container
    blob_client = blob_service_client.get_blob_client(container='tempc2', blob='temp.csv') # BlobClient
    
    with open('temp2.csv', "wb") as download_file:
        download_file.write(blob_client.download_blob().readall())
                
    # delete a blob
    container_client = blob_service_client.get_container_client(container_name)
    container_client.delete_blobs('temp.csv')
                
  • Reference
  • Adam Marczak - Azure for Everyone
  • Documentation
  • azure-sdk-for-python
  • Quickstart: Manage blobs with Python v12 SDK
  • Quickstart: Create, download, and list blobs with Azure CLI