Upload a blob to Azure Storage

This article demonstrates how to upload a blob to Azure Blob Storage using various SDKs and the Azure CLI.

Tip: Azure Blob Storage is a highly scalable and cost-effective object store for storing large amounts of unstructured data.

Prerequisites

Before you begin, make sure you have the following:

  • An Azure subscription.
  • A storage account. If you don't have one, you can create one using the Azure portal or the Azure CLI.
  • The Azure CLI installed (optional, for CLI examples).
  • An Azure SDK library for your preferred language installed (e.g., Azure Blob Storage SDK for .NET, Python, Java, JavaScript).

Upload a Blob using Azure CLI

The Azure CLI provides a simple way to upload blobs.

Azure CLI Command Example
bash
az storage blob upload --account-name  --container-name  --name  --file  --auth-mode login

Replace the placeholders with your actual storage account name, container name, desired blob name, and the path to your local file.

Upload a Blob using Azure SDK for Python

Here's an example of uploading a blob using the Azure Blob Storage SDK for Python.

Python SDK Example
python
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

                            connect_str = ""
                            container_name = ""
                            local_file_name = ""
                            upload_file_path = "./"
                            blob_name = ""

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

                            # Get a client to interact with a specific blob
                            blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

                            print(f"Uploading to blob: {blob_name}")

                            with open(upload_file_path, "rb") as data:
                                blob_client.upload_blob(data)

                            print("Blob uploaded successfully.")

Ensure you have the azure-storage-blob package installed: pip install azure-storage-blob.

Replace the placeholders with your connection string, container name, file name, and blob name.

Upload a Blob using Azure SDK for JavaScript

This example shows how to upload a blob using the Azure Blob Storage SDK for JavaScript (Node.js).

JavaScript SDK Example
javascript
const { BlobServiceClient } = require("@azure/storage-blob");

                            const account = "";
                            const accountKey = "";
                            const containerName = "";
                            const blobName = "";
                            const filePath = "./";

                            async function uploadBlob() {
                                const blobServiceClient = new BlobServiceClient(
                                    `https://${account}.blob.core.windows.net`,
                                    new Azure.Storage.SharedKeyCredential(account, accountKey)
                                );

                                const containerClient = blobServiceClient.getContainerClient(containerName);
                                const blockBlobClient = containerClient.getBlockBlobClient(blobName);

                                console.log(`Uploading to blob: ${blobName}`);
                                await blockBlobClient.uploadFile(filePath);
                                console.log("Blob uploaded successfully.");
                            }

                            uploadBlob().catch((err) => {
                                console.error("Error uploading blob:", err);
                            });

Install the package: npm install @azure/storage-blob.

Replace placeholders with your storage account details, container name, blob name, and file path.

Types of Blobs

Azure Blob Storage supports three types of blobs:

  • Block blobs: Optimized for storing large amounts of unstructured data such as text or binary data. They are made up of blocks of data.
  • Append blobs: Optimized for append operations, such as logging data from a virtual machine.
  • Page blobs: Optimized for random read and write operations. Used for IaaS virtual machine disks.

The upload methods shown above are primarily for block blobs, which are the most common type.

Note: For more advanced scenarios, such as uploading large files in parallel or managing blob properties, refer to the official Azure Blob Storage documentation and SDK examples for your specific language.