Sample for fetchContent (#1392)

* Sample for fetchContent
This commit is contained in:
Victor Vazquez 2021-01-22 16:10:44 -08:00 committed by GitHub
parent 4455ac577c
commit 1aaf1f22df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 152 additions and 0 deletions

View File

@ -3,6 +3,7 @@
Developers like to learn by looking at code, and so the Azure SDK comes with a myriad of code samples in the form of short code snippets, sample applications, and how-to guides. This document describes where to find all these resources.
## Structure of the Repository
The Azure SDK repository is organized in the following folder structure, with the main sample locations highlighted using **bold** font.
`/samples` (this folder)<br>
@ -17,17 +18,26 @@ The Azure SDK repository is organized in the following folder structure, with th
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`/test`<br>
## Getting Started (a.k.a. `Hello World`) Samples
Each package folder contains a package-specific `README.md` file. Most of these `README` files contain `Hello World` code samples illustrating basic usage of the the APIs contained in the package. For example, you can find `Hello World` samples for the `azure-storage-blobs` package [here](https://github.com/Azure/azure-sdk-for-cpp/blob/master/sdk/storage/README.md#code-samples).
## Package Samples and How-To Guides
Each package folder contains a subfolder called `/samples` with additional code samples. These samples can be either short programs contained in `*.c` files, or more complete how-to guides (code samples and some commentary) contained in `*.md` files. You can find shortcuts to main how-to guides in the [**How-To Guides List**](#how-to-guide-list) section below.
## Integration Samples
Simple applications that illustrate the different approaches to integrate the Azure SDK for C++ to your application or library. Each sample contains a README with a description and explanation.
## Sample Applications
Sometimes we want to illustrate how several APIs or even packages work together in a context of a more complete program. For these cases, we created sample applications that you can look at, download, compile, and execute. These application samples are located on
[https://docs.microsoft.com/samples/](https://docs.microsoft.com/samples/).
## How-To Guide List
This section lists how-to guides for the most commonly used APIs and most common scenarios, i.e. this section does not attempt to be a complete directory of guides contained in this repository.
#### General How-To Guides
- How to configure, access, and analyze **logging** information (TODO)

View File

@ -0,0 +1,30 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
# Project set up
cmake_minimum_required(VERSION 3.13)
project(Application-using-storage-blobs LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
#
# Fetch content
#
include(FetchContent)
FetchContent_Declare(azuresdkforcpp
# Set the SDK url path and release TAG
GIT_REPOSITORY https://github.com/Azure/azure-sdk-for-cpp.git
GIT_TAG azure-storage-files-datalake_12.0.0-beta.6)
FetchContent_GetProperties(azuresdkforcpp)
if(NOT azuresdkforcpp_POPULATED)
FetchContent_Populate(azuresdkforcpp)
add_subdirectory(${azuresdkforcpp_SOURCE_DIR} ${azuresdkforcpp_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
add_executable (
application
src/main
)
# Link to Azure SDK
target_link_libraries(application Azure::azure-storage-blobs)

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) Microsoft Corporation. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,42 @@
# Integrating the Azure SDK for C++ into your application using CMake fetch content
This application shows how to integrate the Azure SDK for C++ in your application. It uses CMake fetch content functionality to clone the Azure SDK for C++ repo into your `build` directory. This approach is useful when using CMake to build your application.
## Pre-requisites
Install the [Azure SDK for C++ dependencies](https://github.com/Azure/azure-sdk-for-cpp/blob/master/CONTRIBUTING.md#third-party-dependencies).
- CMake project (min version 3.13).
- C++ version 14 or greater.
## Build
```bash
#
# Building the application.
# Instructions from application root directory.
#
# Create build directory just the first time.
mkdir build
cd build
# Generate and build
# This code assumes that the SDK dependencies were installed with VCPKG
cmake -DCMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake ..
cmake -build .
```
## Run application
Review source code header for `environment variables` that must be set up before running the app.
```bash
#
# Running the Application
# Instructions from inside the build directory.
#
# Run binary (.exe on Windows)
./application
```

View File

@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
/**
* @file
* @brief Application that consumes the Azure SDK for C++.
*
* @remark Set environment variable `STORAGE_CONNECTION_STRING` before running the application.
*
*/
#include <azure/storage/blobs.hpp>
#include <exception>
#include <iostream>
using namespace Azure::Storage::Blobs;
int main(int argc, char* argv[])
{
(void)argc;
(void)argv;
/**************** Container sdk client ************************/
/**************** Create container ************************/
try
{
auto containerClient = BlobContainerClient::CreateFromConnectionString(
std::getenv("STORAGE_CONNECTION_STRING"), "sample");
containerClient.CreateIfNotExists();
/**************** Container sdk client ************************/
/**************** list Blobs (one page) ******************/
auto response = containerClient.ListBlobsSinglePage();
auto blobListPage = response.ExtractValue();
for (auto blob : blobListPage.Items)
{
std::cout << blob.Name << std::endl;
}
}
catch (std::exception& e)
{
std::cout << e.what();
return 1;
}
return 0;
}