Azure SDK for C++

Azure Storage Blob Upload and Download Sample

This sample demonstrates how to upload a file to Azure Blob Storage and then download it back using the Azure SDK for C++. This is a fundamental operation for many cloud-based applications that leverage object storage.

Prerequisites

Sample Code: Uploading a Blob

This C++ code snippet shows the core logic for uploading a file.
UploadBlob.cpp
#include <azure/storage/blobs.hpp>
#include <iostream>
#include <fstream>
#include <string>

int main() {
    // Replace with your actual connection string and container name
    const std::string connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    const std::string container_name = "my-container";
    const std::string blob_name = "my-uploaded-file.txt";
    const std::string local_file_path = "local_file_to_upload.txt";

    // Create a dummy local file for uploading
    std::ofstream ofs(local_file_path);
    ofs << "This is the content of the file to be uploaded to Azure Blob Storage." << std::endl;
    ofs.close();

    try {
        // Create a BlobServiceClient
        auto blob_client_options = Azure::Storage::Blobs::BlobClientOptions();
        // Add any necessary configurations like proxy, retry policy etc.
        // blob_client_options.RetryOptions.MaxRetries = 5;

        Azure::Storage::Blobs::BlobServiceClient blob_service_client(connection_string, blob_client_options);

        // Get a container client
        auto container_client = blob_service_client.GetContainerClient(container_name);

        // Get a blob client
        auto blob_client = container_client.GetBlobClient(blob_name);

        // Open the local file for reading
        std::ifstream upload_file(local_file_path, std::ios::in | std::ios::binary);
        if (!upload_file.is_open()) {
            throw std::runtime_error("Failed to open local file for upload.");
        }

        // Upload the blob
        std::cout << "Uploading blob: " << blob_name << std::endl;
        auto upload_response = blob_client.UploadFrom(upload_file);

        std::cout << "Blob uploaded successfully. ETag: " << upload_response.Value.ETag << std::endl;

    } catch (const Azure::Core::AzureException& ex) {
        std::cerr << "Azure SDK Error: " << ex.what() << std::endl;
        return 1;
    } catch (const std::exception& ex) {
        std::cerr << "Standard Exception: " << ex.what() << std::endl;
        return 1;
    }

    return 0;
}
            

Sample Code: Downloading a Blob

This C++ code snippet shows the core logic for downloading a file.
DownloadBlob.cpp
#include <azure/storage/blobs.hpp>
#include <iostream>
#include <fstream>
#include <string>

int main() {
    // Replace with your actual connection string and container name
    const std::string connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
    const std::string container_name = "my-container";
    const std::string blob_name = "my-uploaded-file.txt";
    const std::string local_download_path = "downloaded_file.txt";

    try {
        // Create a BlobServiceClient
        auto blob_client_options = Azure::Storage::Blobs::BlobClientOptions();
        Azure::Storage::Blobs::BlobServiceClient blob_service_client(connection_string, blob_client_options);

        // Get a container client
        auto container_client = blob_service_client.GetContainerClient(container_name);

        // Get a blob client
        auto blob_client = container_client.GetBlobClient(blob_name);

        // Open the local file for writing
        std::ofstream download_file(local_download_path, std::ios::out | std::ios::binary);
        if (!download_file.is_open()) {
            throw std::runtime_error("Failed to open local file for download.");
        }

        // Download the blob
        std::cout << "Downloading blob: " << blob_name << std::endl;
        auto download_response = blob_client.DownloadTo(download_file);

        std::cout << "Blob downloaded successfully to: " << local_download_path << std::endl;

    } catch (const Azure::Core::AzureException& ex) {
        std::cerr << "Azure SDK Error: " << ex.what() << std::endl;
        return 1;
    } catch (const std::exception& ex) {
        std::cerr << "Standard Exception: " << ex.what() << std::endl;
        return 1;
    }

    return 0;
}
            

Important: Remember to replace YOUR_AZURE_STORAGE_CONNECTION_STRING with your actual Azure Storage account connection string and ensure the container specified (my-container in this example) exists and is accessible.

Explanation

The sample code utilizes the azure-storage-blobs C++ SDK package.

Further Enhancements