Azure Logo

Azure Storage Blob Upload Tutorial

Overview

This tutorial guides you through the process of uploading files to Azure Blob Storage using various methods. Blob storage is a cost-effective and highly scalable object storage solution for the cloud.

Prerequisites

Methods for Uploading Blobs

There are several ways to upload blobs to Azure Storage. We'll cover the most common ones:

1. Using the Azure Portal

The Azure portal provides a user-friendly interface for managing your storage account and uploading files.

1

Navigate to your storage account in the Azure portal.

2

Select "Containers" from the left-hand menu.

3

Click on the container where you want to upload your blob.

4

Click the "Upload" button. A panel will appear to select files.

5

Choose the file(s) you want to upload and click "Upload".

Note: You can upload multiple files and folders simultaneously through the portal.

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for automating storage management tasks.

1

Ensure you have Azure CLI installed and are logged in to your Azure account:

az login
2

Use the az storage blob upload command. Replace placeholders with your actual values:

az storage blob upload \
    --account-name  \
    --container-name  \
    --name  \
    --file  \
    --auth-mode login

Explanation:

  • <YOUR_STORAGE_ACCOUNT_NAME>: The name of your Azure storage account.
  • <YOUR_CONTAINER_NAME>: The name of the container within your storage account.
  • <BLOB_NAME>: The name you want to give to the blob in Azure Storage.
  • <PATH_TO_LOCAL_FILE>: The full path to the file on your local machine.
  • --auth-mode login: Uses your Azure AD credentials for authentication. Alternatively, you can use connection strings or shared access signatures (SAS).
Tip: For uploading large files or directories, consider using the azcopy command-line utility for optimized transfers.

3. Using Azure PowerShell

Azure PowerShell provides another scripting environment for managing Azure resources.

1

Ensure you have the Azure PowerShell module installed and are logged in:

Connect-AzAccount
2

Use the Set-AzStorageBlobContent cmdlet. You might need to retrieve your storage context first.

# Get storage account context
$storageAccount = Get-AzStorageAccount -ResourceGroupName "" -Name ""
$storageContext = $storageAccount.Context

# Upload blob
Set-AzStorageBlobContent `
    -File "" `
    -Container "" `
    -Blob "" `
    -Context $storageContext

Explanation:

  • <YOUR_RESOURCE_GROUP_NAME>: The name of the resource group containing your storage account.
  • <YOUR_STORAGE_ACCOUNT_NAME>: The name of your Azure storage account.
  • <PATH_TO_LOCAL_FILE>: The full path to the file on your local machine.
  • <YOUR_CONTAINER_NAME>: The name of the container.
  • <BLOB_NAME>: The desired name for the blob.

4. Using Azure Storage SDKs

For programmatic uploads within applications, you can use the Azure Storage SDKs available for various programming languages.

Here's a conceptual example using Python:

1

Install the Azure Blob Storage SDK for Python:

pip install azure-storage-blob
2

Use the following Python code. Replace placeholders with your actual values or connection string:

from azure.storage.blob import BlobServiceClient, ContentSettings

connect_str = ""
container_name = ""
local_file_name = ""
blob_name = ""

# Create the BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# Create a blob client
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

# Upload the blob
with open(local_file_name, "rb") as data:
    blob_client.upload_blob(data, content_settings=ContentSettings(content_type='application/octet-stream'))

print(f"Successfully uploaded {local_file_name} to {container_name}/{blob_name}")

Note: Ensure your connection string is kept secure and is not committed to source control.

Similar SDKs are available for .NET, Java, JavaScript, and Go.

Next Steps