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
- An Azure Subscription
- A Blob Storage account
- The Azure SDK for C++ libraries installed.
- A C++ compiler (e.g., Visual Studio, g++)
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.
- Client Initialization: We start by creating a
BlobServiceClientusing the storage account's connection string. You can also provide optionalBlobClientOptionsfor configuring aspects like retry policies or custom endpoints. - Container and Blob Clients: From the
BlobServiceClient, we obtain aContainerClientfor the specific container and then aBlobClientfor the target blob within that container. - File Handling: Standard C++ file streams (
std::ifstreamfor reading,std::ofstreamfor writing) are used to interact with local files. - Uploading: The
BlobClient::UploadFrom()method takes an input file stream and uploads its content to the specified blob. - Downloading: The
BlobClient::DownloadTo()method takes an output file stream and downloads the blob's content into it. - Error Handling: Both snippets include basic try-catch blocks to handle potential
Azure::Core::AzureExceptionerrors and general standard exceptions.
Further Enhancements
- Implement more robust error handling and logging.
- Explore other upload/download options like uploading in chunks, setting blob properties (e.g., content type), or using shared access signatures (SAS).
- Integrate this code into a larger application context.