Azure Storage Files Shares Client Library for C++
Azure File Shares offers fully managed file shares in the cloud that are accessible via the industry standard Server Message Block (SMB) protocol. Azure file shares can be mounted concurrently by cloud or on-premises deployments of Windows, Linux, and macOS. Additionally, Azure file shares can be cached on Windows Servers with Azure File Sync for fast access near where the data is being used.
Install the package
The easiest way to acquire the C++ SDK is leveraging vcpkg package manager. See the corresponding Azure SDK for C++ readme section.
To install Azure Storage packages via vcpkg:
vcpkg install azure-storage-files-shares-cpp
Then, use in your CMake file:
find_package(azure-storage-files-shares-cpp CONFIG REQUIRED)
target_link_libraries(<your project name> PRIVATE Azure::azure-storage-files-shares)
Prerequisites
You need an Azure subscription and a Storage Account to use this package.
To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI.
Build from Source
First, download the repository to your local folder:
git clone https://github.com/Azure/azure-sdk-for-cpp.git
Create a new folder under the root directory of local cloned repo, switch into this folder and run below commands:
Windows:
cmake .. -A x64
cmake --build . --target azure-storage-files-shares
or Unix:
cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake --build . --target azure-storage-files-shares
Azure file shares can be used to:
- Completely replace or supplement traditional on-premises file servers or NAS devices.
- "Lift and shift" applications to the cloud that expect a file share to store file application or user data.
- Simplify new cloud development projects with shared application settings, diagnostic shares, and Dev/Test/Debug tool file shares.
Learn more about options for authentication (including Connection Strings, Shared Key, Shared Key Signatures, Active Directory, and anonymous public access) in our samples.
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts
Client Options | Accessing the response | Long-running operations | Handling failures
Create a share and upload a file
{C++}
const std::string shareName = "sample-share";
const std::string directoryName = "sample-directory";
const std::string fileName = "sample-file";
const std::string localFilePath = "<path_to_local_file>";
// Get a reference to a share and then create it
ShareClient shareClient = ShareClient::CreateFromConnectionString(connectionString, shareName);
shareClient.CreateIfNotExists();
// Get a reference to a directory and create it
ShareDirectoryClient directoryClient = shareClient.GetRootDirectoryClient().GetSubdirectoryClient(directoryName);;
directoryClient.CreateIfNotExists();
// Get a reference to a file and upload it
ShareFileClient fileClient = directoryClient.GetFileClient(fileName);
// upload from local file
fileClient.UploadFrom(localFilePath);
// or upload from memory buffer
fileClient.UploadFrom(bufferPtr, bufferLength);
Download a file
{C++}
// download to local file
fileClient.DownloadTo(localFilePath);
// or download to memory buffer
fileClient.DownloadTo(bufferPtr, bufferLength);
Traverse a share
{C++}
std::vector<ShareDirectoryClient> remaining;
remaining.push_back(shareClient.GetRootDirectoryClient());
while (remaining.size() > 0)
{
auto& directoryClient = remaining.back();
remaining.pop_back();
for (auto page = directoryClient.ListFilesAndDirectories(); page.HasPage();
page.MoveToNextPage())
{
for (auto& file : page.Files)
{
std::cout << "file: " << file.Name << std::endl;
}
for (auto& directory : page.Directories)
{
std::cout << "directory: " << directory.Name << std::endl;
remaining.push_back(directoryClient.GetSubdirectoryClient(directory.Name));
}
}
}
All Azure Storage File Shares service operations will throw a StorageException on failure with helpful ErrorCodes. Many of these errors are recoverable.
{C++}
try
{
shareClient.Delete();
}
catch (Azure::Storage::StorageException& e)
{
if (e.ErrorCode == "ShareNotFound")
{
// ignore the error if the file share does not exist.
}
else
{
// handle other errors here
}
}
Get started with our File samples:
See the Storage CONTRIBUTING.md for details on building, testing, and contributing to these libraries.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
ncG1vNJzZmiZqqq%2Fpr%2FDpJuom6Njr627wWeaqKqVY8SqusOorqxmnprBcHDWnplom6ClfKLG1KucZqukpL%2Bis8RmnaKklah6tLTAq5ysZ2Fne3V6j2igp5yVrXupwMyl