Introducing the new Azure SDK for C++ Beta
Jeffrey
The new Azure SDK for C++ is idiomatic to the C++ language and ensures consistency in behavior and API surface when communicating with Azure services.
Some of the key features of the Azure C++ SDK are:
Customers of our SDK compile our source code along with their own
The SDK is easily consumable by environments using CMake
We target C++ 14 and test for x86, x64, ARM32, and ARM64 CPU architectures using gcc, clang, XCode, & MS Visual C++ compilers
We support Linux, Windows, and Mac platforms
We fully embrace exception handling to report errors from SDK methods
We offer very few abstractions making our code easy to understand and debug
Azure Core
At the heart of our SDK is what we refer to as Azure Core. This code defines several data types and functions for use by the client libraries that build on top of it, such as the Azure Storage Blobs client library. Here are just some of Azure Core’s features:
A replaceable HTTP stack allowing customers to select and use the HTTP stack that they desire. We ship a libcurl transport adapter which enables our SDK to work on all supported CPU architectures and OSes. We will soon have a WinHTTP transport adapter specifically for Windows.
Like our other language SDKs, Azure Core offers an HTTP pipeline of policies which can be configured at runtime.
All I/O operations are cancelable using our own Context mechanism.
We have our own easy-to-use
BodyStream
base class enabling the upload and download of byte streams typically used by blobs and files. Using the decorator pattern, you can create your ownBodyStream
-derived classes to compose features such as progress reporting, encryption, compression, and so on.
In addition to the above features, Azure Core provides features available to client libraries written to access other Azure services. Customers use these features indirectly by way of interacting with a client library. By providing these features in Azure Core, the client libraries built on top of us share a common implementation and many features behave identically across client libraries. For example, Azure Core offers a standard set of credential types and an HTTP pipeline with logging, retry, and telemetry policies.
Example Code using the C++ Storage Blob Client Library
The code below demonstrates how to create a storage blob container, create a blob in that container by uploading a data buffer in memory, and how to download the blob’s data back to a memory buffer. The comments in the code describe what is happening.
#include "azure/storage/blobs/blob.hpp"
#include <iostream>
#include <string>
using namespace Azure::Storage::Blobs;
int main()
{
// Create a BlobContainerClient from a connection string & container name
auto containerClient = BlobContainerClient::CreateFromConnectionString(
"[StorageConnectionString]",
"sample-container");
try
{
containerClient.Create(); // Attempt to create the blob container
}
catch (std::runtime_error& e)
{
// The container may already exist
std::cout << e.what() << std::endl;
return -1;
}
// Create a BlockBlobClient from a container & blob name
BlockBlobClient blobClient = containerClient.GetBlockBlobClient(
"sample-blob");
{
// Create blob whose content is the specified data buffer
std::string blobContent;
blobContent.resize(50 * 1024ULL * 1024, 'x'); // 50 MB of x
blobClient.UploadFrom(
reinterpret_cast<const uint8_t*>(blobContent.data()),
blobContent.size());
}
{
// Download the blob’s contents to a data buffer
blobClient.DownloadTo(
reinterpret_cast<uint8_t*>(&blobContent[0]), blobContent.size());
}
return 0;
}
Azure SDK Blog Contributions
Thank you for reading this Azure SDK blog post! We hope that you learned something new and welcome you to share this post. We are open to Azure SDK blog contributions. Please contact us at azsdkblog@microsoft.com with your topic and we’ll get you setup as a guest blogger.
Azure SDK Links
- Azure SDK Website: aka.ms/azsdk
- Azure SDK Intro (3 minute video): aka.ms/azsdk/intro
- Azure SDK Intro Deck (PowerPoint deck): aka.ms/azsdk/intro/deck
- Azure SDK Releases: aka.ms/azsdk/releases
- Azure SDK Blog: aka.ms/azsdk/blog
- Azure SDK Twitter: twitter.com/AzureSDK
- Azure SDK Design Guidelines: aka.ms/azsdk/guide
- Azure SDKs & Tools: azure.microsoft.com/downloads
- Azure SDK Central Repository: github.com/azure/azure-sdk
- Azure SDK for .NET: github.com/azure/azure-sdk-for-net
- Azure SDK for Java: github.com/azure/azure-sdk-for-java
- Azure SDK for Python: github.com/azure/azure-sdk-for-python
- Azure SDK for JavaScript/TypeScript: github.com/azure/azure-sdk-for-js
- Azure SDK for Android: github.com/Azure/azure-sdk-for-android
- Azure SDK for iOS: github.com/Azure/azure-sdk-for-ios
- Azure SDK for Go: github.com/Azure/azure-sdk-for-go
- Azure SDK for C: github.com/Azure/azure-sdk-for-c
- Azure SDK for C++: github.com/Azure/azure-sdk-for-cpp
2 comments
It is the year 2021, and the Q is 2: 2021Q2
Let’s assume there are 4 Azure customers: A,B,C and D. Customers A,B,C have written DLL’s using Azure SDK as above. The error handling (as advised) is all C++ exceptions. Customer D is using all of those dll’s in her Azure service also developed using the same Azure SDK.
What can possibly go wrong?
All the DLLs in question need to be built with the same, or at least an ABI compatible, compiler. (e.g. VS2015 through VS2019 are currently compatible, VS2013 is not). C++ exceptions rely on thread local variables that live inside the C runtime bits. All the DLLs in question need to be built with the same version of our Azure Core library since this is where our base class exception type lives.