Creating containers and blobs in Azure involves a series of steps that can be done through the Azure portal, Azure CLI, or programmatically using SDKs such as .NET, Python, or Java. Below are the detailed steps to create containers and blobs using different methods.

Using Azure Portal

Create a Container

  1. Navigate to the Storage Account:
    • Log in to the Azure Portal.
    • Go to your storage account or create a new one.
  2. Create a Container:
    • In the left-hand menu, under the “Data storage” section, click on “Containers”.
    • Click on the “+ Container” button.
    • Provide a name for your container.
    • Set the public access level (Private, Blob, or Container).
    • Click “Create”.

Upload a Blob

  1. Navigate to the Container:
    • Click on the container you just created.
  2. Upload a Blob:
    • Click on the “Upload” button.
    • Select the file you want to upload.
    • Set the required options and click “Upload”.

Using Azure CLI

Prerequisites

  • Install the Azure CLI.
  • Sign in to Azure using az login.

Create a Container

# Replace <storage-account-name> and <container-name> with your values
az storage container create \
–account-name <storage-account-name> \
–name <container-name>

Upload a Blob

# Replace <storage-account-name>, <container-name>, and <file-path> with your values
az storage blob upload \
–account-name <storage-account-name> \
–container-name <container-name> \
–name <blob-name> \
–file <file-path>

Using Python SDK

Prerequisites

  • Install the Azure Storage Blob SDK for Python:

pip install azure-storage-blob

Create a Container and Upload a Blob

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

# Replace with your connection string
connection_string = “your_connection_string”

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

# Create a container
container_name = “your_container_name”
container_client = blob_service_client.create_container(container_name)

# Upload a blob
blob_name = “your_blob_name”
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

# Open the file and upload
with open(“your_file_path”, “rb”) as data:
blob_client.upload_blob(data)

Using .NET SDK

Prerequisites

  • Install the Azure.Storage.Blobs package:

dotnet add package Azure.Storage.Blobs

Create a Container and Upload a Blob

using Azure.Storage.Blobs;
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
private static string connectionString = “your_connection_string”;

static async Task Main(string[] args)
{
// Create the BlobServiceClient
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

// Create a container
string containerName = “your_container_name”;
BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);

// Upload a blob
string blobName = “your_blob_name”;
BlobClient blobClient = containerClient.GetBlobClient(blobName);

// Open the file and upload
using FileStream uploadFileStream = File.OpenRead(“your_file_path”);
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();
}
}

These are the common methods for creating containers and uploading blobs in Azure Storage. Each method can be further customized based on specific needs and requirements.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed