Working with Files in Azure Blob Storage

A comprehensive guide to uploading, downloading, and managing files using Azure Blob Storage.

On This Page

Introduction

Azure Blob Storage is Microsoft's object storage solution for the cloud. It is optimized for storing massive amounts of unstructured data, such as text or binary data. Unstructured data is data that doesn't adhere to a particular data model or definition, such as images, videos, audio files, or any other type of media file, documents, or backup data.

This document will guide you through the essential operations for working with files (blobs) in Azure Blob Storage, covering common use cases and providing code examples for popular programming languages and tools.

Prerequisites

Getting Started

Before you can work with files, you need to connect to your Azure Storage account. This typically involves using your storage account name and one of its access keys, or a Shared Access Signature (SAS) token.

Note: For production environments, consider using Azure Active Directory (Azure AD) for authentication and authorization for enhanced security.

Using the Azure Portal

The Azure portal provides a user-friendly interface to upload, download, and manage blobs directly. Navigate to your storage account, select 'Containers', choose a container, and then click 'Upload' or 'Download' as needed.

Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources. You can use it for various blob operations.


# Login to your Azure account
az login

# Set your subscription (if you have multiple)
az account set --subscription ""

# List storage accounts
az storage account list

# Create a container (if it doesn't exist)
az storage container create --name mycontainer --account-name mystorageaccount --public-access off
            

Uploading Files

Uploading files, often referred to as uploading blobs, is a fundamental operation. Azure Blob Storage supports uploading various types of files, including block blobs, page blobs, and append blobs. For most file storage scenarios, block blobs are the most common choice.

Upload using Azure CLI


# Upload a single file
az storage blob upload --account-name mystorageaccount --container-name mycontainer --file "local/path/to/your/file.txt" --name "destination/blob/name.txt"

# Upload multiple files from a directory
az storage blob upload-batch --account-name mystorageaccount --destination mycontainer --source "local/directory/" --pattern "*.txt"
            

Upload using Azure SDK for Python


from azure.storage.blob import BlobServiceClient

connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

container_client = blob_service_client.get_container_client("mycontainer")

# Upload a file
blob_name = "destination/blob/name.txt"
local_file_path = "local/path/to/your/file.txt"

with open(local_file_path, "rb") as data:
    container_client.upload_blob(name=blob_name, data=data)
print(f"Uploaded {blob_name} to container.")
            
Tip: Replace YOUR_AZURE_STORAGE_CONNECTION_STRING with your actual connection string found in your storage account's access keys in the Azure portal.

Downloading Files

Downloading files from Azure Blob Storage is as straightforward as uploading. You can download a single blob to a local file or stream its content.

Download using Azure CLI


# Download a single blob
az storage blob download --account-name mystorageaccount --container-name mycontainer --name "destination/blob/name.txt" --file "local/path/to/save/file.txt"

# Download all blobs from a container
az storage blob download-batch --account-name mystorageaccount --destination "local/directory/" --container-name mycontainer
            

Download using Azure SDK for Python


from azure.storage.blob import BlobServiceClient

connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

container_client = blob_service_client.get_container_client("mycontainer")

blob_name = "destination/blob/name.txt"
local_file_path = "local/path/to/save/downloaded_file.txt"

# Download blob content
blob_client = container_client.get_blob_client(blob_name)
with open(local_file_path, "wb") as download_file:
    download_file.write(blob_client.download_blob().readall())
print(f"Downloaded {blob_name} to {local_file_path}.")
            

Managing Files

Beyond uploading and downloading, you can perform various management operations on your blobs.

List Blobs

View the contents of a container.


az storage blob list --account-name mystorageaccount --container-name mycontainer
            

# Using Azure SDK for Python
for blob_item in container_client.list_blobs():
    print(f"Blob name: {blob_item.name}")
            

Delete Blobs

Remove blobs from a container.


az storage blob delete --account-name mystorageaccount --container-name mycontainer --name "blob/to/delete.txt"
            

# Using Azure SDK for Python
container_client.delete_blob("blob/to/delete.txt")
print("Blob deleted successfully.")
            

Copy Blobs

Copy blobs within the same storage account or between different storage accounts.


# Copy within the same account
az storage blob copy --account-name mystorageaccount --source-container mycontainer --source-blob "source/blob.txt" --destination-container mycontainer --destination-blob "copied/blob.txt"
            
Note: For copying between accounts, you'll need appropriate credentials and potentially use the Copy Blob API or SDK methods.

Set Blob Properties and Metadata

Customize blob properties like content type or add custom metadata.


# Using Azure SDK for Python
blob_client = container_client.get_blob_client("myblob.txt")
blob_client.set_blob_properties(content_settings={'content_type': 'text/plain'})
blob_client.set_blob_metadata({'custom-key': 'custom-value'})
print("Blob properties and metadata updated.")
            

Best Practices

Further Reading

Key API References