Create a Blob in Azure Storage

This article walks you through the process of creating a blob in Azure Blob Storage using various methods, including the Azure portal, Azure CLI, Azure PowerShell, and SDKs.

Prerequisites: Ensure you have an Azure subscription and an Azure Storage account. If you don't have one, you can create one for free.

1. Using the Azure Portal

The Azure portal provides a user-friendly interface for managing your Azure resources, including creating blobs.

  1. Navigate to your storage account in the Azure portal.
  2. Under "Data storage," select "Containers."
  3. Select the container where you want to upload the blob.
  4. Click the "Upload" button.
  5. Browse to select the file you want to upload.
  6. Configure optional settings like access tier and metadata.
  7. Click "Upload."

2. Using Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources from your command line.

2.1. Install Azure CLI

If you haven't already, install the Azure CLI from the official Azure CLI documentation.

2.2. Log in to Azure

az login

2.3. Upload a Blob

Use the az storage blob upload command to upload a file.

Azure CLI
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 local path to your file. The --auth-mode login option uses your Azure AD credentials for authentication.

3. Using Azure PowerShell

Azure PowerShell provides a scripting environment for managing Azure resources.

3.1. Install Azure PowerShell

If you haven't already, install the Azure PowerShell module from the official Azure PowerShell documentation.

3.2. Connect to Azure

Connect-AzAccount

3.3. Upload a Blob

Use the Set-AzStorageBlobContent cmdlet to upload a blob.

Azure PowerShell
# Get storage account context
$ctx = New-AzStorageContext -StorageAccountName "" -AuthenticationMode Login
# Upload blob
Set-AzStorageBlobContent -File "" -Container "" -Blob "" -Context $ctx

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

4. Using SDKs

Azure provides SDKs for various programming languages to interact with Azure Storage programmatically.

4.1. .NET SDK Example

Here's a C# example using the Azure.Storage.Blobs NuGet package.

C#
using Azure.Storage.Blobs;
using System;
using System.IO;

public class BlobUploader
{
    public static void UploadBlob(string connectionString, string containerName, string blobName, string filePath)
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        using (FileStream uploadFileStream = File.OpenRead(filePath))
        {
            blobClient.Upload(uploadFileStream, true); // Overwrite if exists
            Console.WriteLine($"Successfully uploaded blob '{blobName}' to container '{containerName}'.");
        }
    }

    // Example usage:
    // string connectionString = "YOUR_CONNECTION_STRING";
    // UploadBlob(connectionString, "mycontainer", "myblob.txt", "C:\\path\\to\\myfile.txt");
}

You'll need to install the Azure.Storage.Blobs NuGet package.

Tip: For other languages like Python, Java, or Node.js, refer to the respective Azure SDK documentation for Blob Storage.

Best Practices