Upload blobs to Azure Blob Storage
This document guides you through the process of uploading blobs to Azure Blob Storage using various methods, including the Azure portal, Azure CLI, Azure SDKs, and REST API.
1. Using the Azure Portal
The Azure portal offers a user-friendly interface for uploading blobs. This is ideal for manual uploads or for testing purposes.
- Navigate to your storage account in the Azure portal.
- Select the container to which you want to upload blobs.
- Click the Upload button.
- Select the files you wish to upload from your local machine.
- Click Upload.
The portal also allows you to upload entire folders and manage blob properties during the upload process.
2. Using Azure CLI
The Azure Command-Line Interface (CLI) provides powerful scripting capabilities for managing Azure resources, including uploading blobs.
Prerequisites
- Install the Azure CLI.
- Sign in to your Azure account:
az login
Upload a single blob
Use the az storage blob upload command to upload a file:
az storage blob upload \
--account-name <your-storage-account-name> \
--container-name <your-container-name> \
--name <blob-name> \
--file <path-to-local-file> \
--auth-mode login
Upload multiple blobs
To upload all files from a local directory to a container:
az storage blob upload-batch \
--account-name <your-storage-account-name> \
--destination <your-container-name> \
--source <path-to-local-directory> \
--auth-mode login
Replace placeholders like <your-storage-account-name>, <your-container-name>, <blob-name>, and <path-to-local-file> with your actual values.
--auth-mode login for Azure AD authentication, or provide a connection string or SAS token for other authentication methods.
3. Using Azure SDKs
Azure Storage SDKs are available for various programming languages, allowing you to integrate blob upload functionality into your applications.
Python Example
Here's a basic example using the Azure Blob Storage client library for Python:
from azure.storage.blob import BlobServiceClient
CONNECTION_STRING = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
CONTAINER_NAME = "mycontainer"
LOCAL_FILE_PATH = "local_file.txt"
BLOB_NAME = "my_uploaded_blob.txt"
def upload_blob_from_path(connection_string, container_name, file_path, blob_name):
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
with open(file_path, "rb") as data:
blob_client.upload_blob(data)
print(f"Blob '{blob_name}' uploaded successfully.")
if __name__ == "__main__":
# Ensure you have a container named 'mycontainer' or change the name.
# You can create it using:
# blob_service_client = BlobServiceClient.from_connection_string(CONNECTION_STRING)
# container_client = blob_service_client.create_container(CONTAINER_NAME)
# Create a dummy local file for demonstration
with open(LOCAL_FILE_PATH, "w") as f:
f.write("This is the content of the local file to be uploaded.")
upload_blob_from_path(CONNECTION_STRING, CONTAINER_NAME, LOCAL_FILE_PATH, BLOB_NAME)
Remember to install the library: pip install azure-storage-blob and replace placeholders with your actual connection string and names.
Other SDKs
Similar examples exist for other SDKs, including:
4. Using Azure Storage REST API
For maximum flexibility, you can interact directly with Azure Blob Storage using its REST API. This involves sending HTTP requests to the service endpoints.
Put Blob Operation
The primary operation for uploading a blob is Put Blob.
Request URL:
https://<storage-account-name>.blob.core.windows.net/<container-name>/<blob-name>?comp=block
HTTP Method: PUT
Headers:
x-ms-version: 2020-04-08(or a later supported version)x-ms-blob-type: BlockBlobContent-Length: <size-of-blob-in-bytes>Authorization: SharedKey <storage-account-name>:<signature>(or other authentication)
Request Body: The binary content of the blob.
For detailed information on constructing REST API requests, refer to the official Put Blob documentation.
Choosing the Right Method
The best method for uploading blobs depends on your specific needs:
- Azure Portal: Quick manual uploads, visual management.
- Azure CLI: Scripting, automation, repetitive tasks.
- Azure SDKs: Application integration, programmatic control.
- REST API: Advanced control, integration with unsupported languages, custom tooling.
Explore these options to find the most efficient way to manage your data in Azure Blob Storage.