Azure

Azure Blob Storage in C#

Introduction

Azure Blob Storage is a scalable cloud storage service that enables builders to retailer and handle huge quantities of unstructured information. It is a vital part for varied functions that cope with pictures, movies, paperwork, backups, and extra. With its reliability, sturdiness, and accessibility, Azure Blob Storage is a go-to selection for cloud-based storage options.

Overview of Azure Blob Storage

Azure Blob Storage organizes information into containers. Every container can maintain an infinite variety of blobs, that are the precise recordsdata. Blobs might be of three sorts: Block blobs, Append blobs, and Web page blobs. Block blobs are appropriate for general-purpose storage of recordsdata, whereas Append blobs are perfect for append-only eventualities like logging. Web page blobs are optimized for random learn/write operations and are sometimes used to retailer digital laborious drive (VHD) recordsdata.

Working with Azure Blob Storage in C#

To make the most of Azure Blob Storage in C#, you may want the Azure Storage SDK. When you’re utilizing Visual Studio, you possibly can set up the required bundle through the NuGet Package deal Supervisor. Here is a step-by-step information demonstrating how one can carry out primary operations:

1. Setting Up Azure Storage Connection

First, guarantee you could have an Azure subscription and have created a Storage Account within the Azure portal. Upon getting the account particulars, set up the Azure.Storage.Blobs bundle.

utilizing Azure.Storage.Blobs;

public class AzureBlobStorageService
{
    personal string connectionString = "YourStorageAccountConnectionString";
    personal BlobServiceClient blobServiceClient;

    public AzureBlobStorageService()
    {
        blobServiceClient = new BlobServiceClient(connectionString);
    }
}

Exchange “YourStorageAccountConnectionString” with the connection string on your Azure Storage Account.

2. Making a Blob Container

Now, let’s create a container throughout the storage account.

public async Activity CreateContainerAsync(string containerName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    await containerClient.CreateIfNotExistsAsync();
}

3. Importing a Blob

Here is an instance of how one can add a file (blob) to a container.

public async Activity UploadBlobAsync(string containerName, string blobName, string filePath)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    utilizing FileStream fs = File.OpenRead(filePath);
    await blobClient.UploadAsync(fs, true);
}

4. Downloading a Blob

To retrieve a blob from the container, use the next technique.

public async Activity DownloadBlobAsync(string containerName, string blobName, string downloadPath)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    BlobDownloadInfo blobDownloadInfo = await blobClient.DownloadAsync();

    utilizing FileStream fs = File.OpenWrite(downloadPath);
    await blobDownloadInfo.Content material.CopyToAsync(fs);
}

5. Itemizing Blobs in a Container

To checklist all of the blobs inside a container, you should use the next technique.

public async Activity<Listing<string>> ListBlobsAsync(string containerName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    var blobs = new Listing<string>();

    await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
    {
        blobs.Add(blobItem.Identify);
    }

    return blobs;
}

This snippet retrieves the names of all blobs throughout the specified container and returns an inventory of strings containing the blob names.

6. Deleting a Blob

To delete a selected blob from a container.

public async Activity DeleteBlobAsync(string containerName, string blobName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    await blobClient.DeleteIfExistsAsync();
}

This technique deletes the required blob from the container.

7. Deleting a Container

To take away a whole container from the storage account.

public async Activity DeleteContainerAsync(string containerName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

    await containerClient.DeleteIfExistsAsync();
}

8. Accessing Blob Metadata

Fetching metadata related to a selected blob.

public async Activity<IDictionary<string, string>> GetBlobMetadataAsync(string containerName, string blobName)
{
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    BlobProperties properties = await blobClient.GetPropertiesAsync();
    return properties.Metadata;
}

This snippet fetches the metadata related to the required blob within the container.

Conclusion

Azure Blob Storage supplies a dependable and scalable resolution for storing varied sorts of unstructured information within the cloud. Utilizing C# with the Azure.Storage.Blobs bundle, builders can simply carry out elementary operations akin to creating containers, importing, and downloading blobs. This allows the seamless integration of cloud-based storage capabilities inside functions. By following the offered code snippets, builders can harness the ability of Azure Blob Storage of their C# functions.

Know extra about our firm at Skrots. Know extra about our providers at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

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

Back to top button