From 1b814a87574fd70c5f9f1c5675cc4aecc725ab33 Mon Sep 17 00:00:00 2001 From: JinmingHu Date: Fri, 19 Nov 2021 09:59:34 +0800 Subject: [PATCH] restructure storage samples (#3113) * restructure storage samples * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Victor Vazquez * u * u2 * fix link * clang-format Co-authored-by: Victor Vazquez --- sdk/storage/CMakeLists.txt | 4 - sdk/storage/README.md | 6 +- .../azure-storage-blobs/CMakeLists.txt | 13 +--- .../samples/CMakeLists.txt | 16 ++++ .../blob_getting_started.cpp | 33 ++++++-- .../blob_list_operation.cpp | 33 ++++++-- .../{sample => samples}/blob_sas.cpp | 54 +++++++++---- .../transactional_checksum.cpp | 31 ++++++-- .../azure-storage-common/CMakeLists.txt | 11 --- .../azure-storage-common/sample/main.cpp | 78 ------------------- .../sample/samples_common.hpp | 42 ---------- .../CMakeLists.txt | 8 +- .../samples/CMakeLists.txt | 7 ++ .../datalake_getting_started.cpp | 33 ++++++-- .../azure-storage-files-shares/CMakeLists.txt | 8 +- .../samples/CMakeLists.txt | 7 ++ .../file_share_getting_started.cpp | 33 ++++++-- .../azure-storage-queues/CMakeLists.txt | 9 +-- .../samples/CMakeLists.txt | 10 +++ .../queue_encode_message.cpp | 30 ++++++- .../queue_getting_started.cpp | 29 ++++++- 21 files changed, 273 insertions(+), 222 deletions(-) create mode 100644 sdk/storage/azure-storage-blobs/samples/CMakeLists.txt rename sdk/storage/azure-storage-blobs/{sample => samples}/blob_getting_started.cpp (62%) rename sdk/storage/azure-storage-blobs/{sample => samples}/blob_list_operation.cpp (69%) rename sdk/storage/azure-storage-blobs/{sample => samples}/blob_sas.cpp (50%) rename sdk/storage/azure-storage-blobs/{sample => samples}/transactional_checksum.cpp (75%) delete mode 100644 sdk/storage/azure-storage-common/sample/main.cpp delete mode 100644 sdk/storage/azure-storage-common/sample/samples_common.hpp create mode 100644 sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt rename sdk/storage/azure-storage-files-datalake/{sample => samples}/datalake_getting_started.cpp (73%) create mode 100644 sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt rename sdk/storage/azure-storage-files-shares/{sample => samples}/file_share_getting_started.cpp (62%) create mode 100644 sdk/storage/azure-storage-queues/samples/CMakeLists.txt rename sdk/storage/azure-storage-queues/{sample => samples}/queue_encode_message.cpp (66%) rename sdk/storage/azure-storage-queues/{sample => samples}/queue_getting_started.cpp (79%) diff --git a/sdk/storage/CMakeLists.txt b/sdk/storage/CMakeLists.txt index 00ee2a9f8a..aa399f2bc4 100644 --- a/sdk/storage/CMakeLists.txt +++ b/sdk/storage/CMakeLists.txt @@ -17,10 +17,6 @@ if(BUILD_TESTING) add_gtest(azure-storage-test) endif() -if(BUILD_STORAGE_SAMPLES) - add_executable(azure-storage-sample) -endif() - add_subdirectory(azure-storage-common) add_subdirectory(azure-storage-blobs) add_subdirectory(azure-storage-files-datalake) diff --git a/sdk/storage/README.md b/sdk/storage/README.md index 7a3ec57951..f56b931535 100644 --- a/sdk/storage/README.md +++ b/sdk/storage/README.md @@ -131,9 +131,9 @@ target_link_libraries( PRIVATE Azure::azure-storage-files-sha ## Code Samples To get started with the coding, please visit the following code samples: -- [How to use Blob Storage from C++](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp) -- [How to use DataLake Gen 2 Storage from C++](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp) -- [How to use File Storage from C++](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp) +- [How to use Blob Storage from C++](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp) +- [How to use DataLake Gen 2 Storage from C++](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp) +- [How to use File Storage from C++](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp) [azsdk_vcpkg_install]: https://github.com/Azure/azure-sdk-for-cpp#download--install-the-sdk diff --git a/sdk/storage/azure-storage-blobs/CMakeLists.txt b/sdk/storage/azure-storage-blobs/CMakeLists.txt index aee7858418..a3217fd80e 100644 --- a/sdk/storage/azure-storage-blobs/CMakeLists.txt +++ b/sdk/storage/azure-storage-blobs/CMakeLists.txt @@ -116,18 +116,9 @@ if(BUILD_TESTING) endif() if(BUILD_STORAGE_SAMPLES) - target_sources( - azure-storage-sample - PRIVATE - sample/blob_getting_started.cpp - sample/blob_list_operation.cpp - sample/blob_sas.cpp - sample/transactional_checksum.cpp - ) - - target_link_libraries(azure-storage-sample PRIVATE azure-storage-blobs) + add_subdirectory(samples) endif() -if (BUILD_PERFORMANCE_TESTS) +if(BUILD_PERFORMANCE_TESTS) add_subdirectory(test/perf) endif() diff --git a/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt b/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt new file mode 100644 index 0000000000..af3079b331 --- /dev/null +++ b/sdk/storage/azure-storage-blobs/samples/CMakeLists.txt @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT + +cmake_minimum_required (VERSION 3.13) + +add_executable(blob-getting-started blob_getting_started.cpp) +target_link_libraries(blob-getting-started PRIVATE azure-storage-blobs) + +add_executable(blob-list-operation blob_list_operation.cpp) +target_link_libraries(blob-list-operation PRIVATE azure-storage-blobs) + +add_executable(blob-sas blob_sas.cpp) +target_link_libraries(blob-sas PRIVATE azure-storage-blobs) + +add_executable(transactional-checksum transactional_checksum.cpp) +target_link_libraries(transactional-checksum PRIVATE azure-storage-blobs) diff --git a/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp b/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp similarity index 62% rename from sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp rename to sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp index 986acf7dfe..8c7c080426 100644 --- a/sdk/storage/azure-storage-blobs/sample/blob_getting_started.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp @@ -1,20 +1,39 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; -SAMPLE(BlobsGettingStarted, BlobsGettingStarted) -void BlobsGettingStarted() + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} + +int main() { using namespace Azure::Storage::Blobs; - std::string containerName = "sample-container"; - std::string blobName = "sample-blob"; - std::string blobContent = "Hello Azure!"; + const std::string containerName = "sample-container"; + const std::string blobName = "sample-blob"; + const std::string blobContent = "Hello Azure!"; auto containerClient = BlobContainerClient::CreateFromConnectionString(GetConnectionString(), containerName); @@ -40,4 +59,6 @@ void BlobsGettingStarted() blobClient.DownloadTo(buffer.data(), buffer.size()); std::cout << std::string(buffer.begin(), buffer.end()) << std::endl; + + return 0; } diff --git a/sdk/storage/azure-storage-blobs/sample/blob_list_operation.cpp b/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp similarity index 69% rename from sdk/storage/azure-storage-blobs/sample/blob_list_operation.cpp rename to sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp index 694a8e13d0..7bc24eed4b 100644 --- a/sdk/storage/azure-storage-blobs/sample/blob_list_operation.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp @@ -1,20 +1,39 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; -SAMPLE(BlobListOperation, BlobListOperation) -void BlobListOperation() + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} + +int main() { using namespace Azure::Storage::Blobs; - std::string containerName = "sample-container"; - std::string blobName = "sample-blob"; - std::string blobContent = "Hello Azure!"; + const std::string containerName = "sample-container"; + const std::string blobName = "sample-blob"; + const std::string blobContent = "Hello Azure!"; { // Create some containers and blobs for test @@ -54,4 +73,6 @@ void BlobListOperation() } } } + + return 0; } diff --git a/sdk/storage/azure-storage-blobs/sample/blob_sas.cpp b/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp similarity index 50% rename from sdk/storage/azure-storage-blobs/sample/blob_sas.cpp rename to sdk/storage/azure-storage-blobs/samples/blob_sas.cpp index 33a43626a7..fa46b669df 100644 --- a/sdk/storage/azure-storage-blobs/sample/blob_sas.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp @@ -1,30 +1,56 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; + + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} -SAMPLE(BlobSas, BlobSas) -void BlobSas() +std::string GetAccountName() { - using namespace Azure::Storage::Blobs; + return Azure::Storage::_internal::ParseConnectionString(GetConnectionString()).AccountName; +} - std::string accountName = GetAccountName(); - std::string accountKey = GetAccountKey(); +std::string GetAccountKey() +{ + return Azure::Storage::_internal::ParseConnectionString(GetConnectionString()).AccountKey; +} + +int main() +{ + using namespace Azure::Storage::Blobs; - std::string containerName = "sample-container"; - std::string blobName = "sample-blob"; - std::string blobContent = "Hello Azure!"; + const std::string containerName = "sample-container"; + const std::string blobName = "sample-blob"; + const std::string blobContent = "Hello Azure!"; // Create a container and a blob for test { - auto credential - = std::make_shared(accountName, accountKey); + auto credential = std::make_shared( + GetAccountName(), GetAccountKey()); auto containerClient = BlobContainerClient( - "https://" + accountName + ".blob.core.windows.net/" + containerName, credential); + "https://" + GetAccountName() + ".blob.core.windows.net/" + containerName, credential); containerClient.CreateIfNotExists(); BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName); blobClient.UploadFrom(reinterpret_cast(blobContent.data()), blobContent.size()); @@ -39,10 +65,10 @@ void BlobSas() sasBuilder.SetPermissions(Azure::Storage::Sas::BlobSasPermissions::Read); std::string sasToken = sasBuilder.GenerateSasToken( - Azure::Storage::StorageSharedKeyCredential(accountName, accountKey)); + Azure::Storage::StorageSharedKeyCredential(GetAccountName(), GetAccountKey())); auto blobClient = BlobClient( - "https://" + accountName + ".blob.core.windows.net/" + containerName + "/" + blobName + "https://" + GetAccountName() + ".blob.core.windows.net/" + containerName + "/" + blobName + sasToken); // We can read the blob diff --git a/sdk/storage/azure-storage-blobs/sample/transactional_checksum.cpp b/sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp similarity index 75% rename from sdk/storage/azure-storage-blobs/sample/transactional_checksum.cpp rename to sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp index e759db2ef0..96e0342358 100644 --- a/sdk/storage/azure-storage-blobs/sample/transactional_checksum.cpp +++ b/sdk/storage/azure-storage-blobs/samples/transactional_checksum.cpp @@ -1,19 +1,38 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; -SAMPLE(TransactionalChecksum, TransactionalChecksum) -void TransactionalChecksum() + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} + +int main() { using namespace Azure::Storage::Blobs; - std::string containerName = "sample-container"; - std::string blobName = "sample-blob"; + const std::string containerName = "sample-container"; + const std::string blobName = "sample-blob"; auto containerClient = BlobContainerClient::CreateFromConnectionString(GetConnectionString(), containerName); @@ -51,4 +70,6 @@ void TransactionalChecksum() { std::cout << "CRC-64 match" << std::endl; } + + return 0; } diff --git a/sdk/storage/azure-storage-common/CMakeLists.txt b/sdk/storage/azure-storage-common/CMakeLists.txt index 77a6a865a1..fb981d455d 100644 --- a/sdk/storage/azure-storage-common/CMakeLists.txt +++ b/sdk/storage/azure-storage-common/CMakeLists.txt @@ -129,14 +129,3 @@ if(BUILD_TESTING) target_link_libraries(azure-storage-test PRIVATE azure-identity) target_include_directories(azure-storage-test PRIVATE test) endif() - -if(BUILD_STORAGE_SAMPLES) - target_sources( - azure-storage-sample - PRIVATE - sample/main.cpp - sample/samples_common.hpp - ) - - target_include_directories(azure-storage-sample PRIVATE sample) -endif() diff --git a/sdk/storage/azure-storage-common/sample/main.cpp b/sdk/storage/azure-storage-common/sample/main.cpp deleted file mode 100644 index b17271cf5e..0000000000 --- a/sdk/storage/azure-storage-common/sample/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright(c) Microsoft Corporation.All rights reserved. -// SPDX - License - Identifier : MIT - -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - -#include -#include -#include - -#include - -#include "samples_common.hpp" - -std::string GetConnectionString() -{ - const static std::string ConnectionString = ""; - - if (!ConnectionString.empty()) - { - return ConnectionString; - } - const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); - if (!envConnectionString.empty()) - { - return envConnectionString; - } - throw std::runtime_error("Cannot find connection string."); -} - -std::string GetAccountName() -{ - return Azure::Storage::_internal::ParseConnectionString(GetConnectionString()).AccountName; -} - -std::string GetAccountKey() -{ - return Azure::Storage::_internal::ParseConnectionString(GetConnectionString()).AccountKey; -} - -int main(int argc, char** argv) -{ - if (argc != 2) - { - printf("Usage: %s \n", argv[0]); - } - else if (std::string(argv[1]) == "All") - { - for (auto sample : Sample::samples()) - { - auto func = sample.second; - func(); - } - return 0; - } - else - { - auto ite = Sample::samples().find(argv[1]); - if (ite == Sample::samples().end()) - { - printf("Cannot find sample %s\n", argv[1]); - } - else - { - auto func = ite->second; - func(); - return 0; - } - } - - printf("\nAvailable sample names:\n All\n"); - for (const auto& i : Sample::samples()) - { - printf(" %s\n", i.first.data()); - } - return 1; -} diff --git a/sdk/storage/azure-storage-common/sample/samples_common.hpp b/sdk/storage/azure-storage-common/sample/samples_common.hpp deleted file mode 100644 index 1bfafeab1f..0000000000 --- a/sdk/storage/azure-storage-common/sample/samples_common.hpp +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright(c) Microsoft Corporation.All rights reserved. -// SPDX - License - Identifier : MIT - -#pragma once - -#include -#include -#include - -std::string GetConnectionString(); -std::string GetAccountName(); -std::string GetAccountKey(); - -class Sample { -public: - virtual ~Sample() = default; - static const std::map>& samples() { return m_samples(); } - -protected: - static void add_sample(std::string sample_name, std::function func) - { - m_samples().emplace(std::move(sample_name), std::move(func)); - } - -private: - static std::map>& m_samples() - { - static std::map> samples_instance; - return samples_instance; - } -}; - -#define SAMPLE(NAME, FUNCTION) \ - void FUNCTION(); \ -\ - class Sample##NAME final : public Sample { \ - public: \ - Sample##NAME() { add_sample(#NAME, FUNCTION); } \ - }; \ - namespace { \ - Sample##NAME Sample##NAME_; \ - } diff --git a/sdk/storage/azure-storage-files-datalake/CMakeLists.txt b/sdk/storage/azure-storage-files-datalake/CMakeLists.txt index c7ebe57953..1c94dfc721 100644 --- a/sdk/storage/azure-storage-files-datalake/CMakeLists.txt +++ b/sdk/storage/azure-storage-files-datalake/CMakeLists.txt @@ -114,11 +114,5 @@ if(BUILD_TESTING) endif() if(BUILD_STORAGE_SAMPLES) - target_sources( - azure-storage-sample - PRIVATE - sample/datalake_getting_started.cpp - ) - - target_link_libraries(azure-storage-sample PRIVATE azure-storage-files-datalake) + add_subdirectory(samples) endif() diff --git a/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt b/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt new file mode 100644 index 0000000000..e8bae76434 --- /dev/null +++ b/sdk/storage/azure-storage-files-datalake/samples/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT + +cmake_minimum_required (VERSION 3.13) + +add_executable(datalake-getting-started datalake_getting_started.cpp) +target_link_libraries(datalake-getting-started PRIVATE azure-storage-files-datalake) diff --git a/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp b/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp similarity index 73% rename from sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp rename to sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp index 65d28563e2..4cbbec7e27 100644 --- a/sdk/storage/azure-storage-files-datalake/sample/datalake_getting_started.cpp +++ b/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp @@ -1,20 +1,39 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; -SAMPLE(DataLakeGettingStarted, DataLakeGettingStarted) -void DataLakeGettingStarted() + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} + +int main() { using namespace Azure::Storage::Files::DataLake; - std::string fileSystemName = "sample-file-system"; - std::string directoryName = "sample-directory"; - std::string fileName = "sample-file"; + const std::string fileSystemName = "sample-file-system"; + const std::string directoryName = "sample-directory"; + const std::string fileName = "sample-file"; auto fileSystemClient = DataLakeFileSystemClient::CreateFromConnectionString(GetConnectionString(), fileSystemName); @@ -57,4 +76,6 @@ void DataLakeGettingStarted() std::vector downloaded = result.Value.Body->ReadToEnd(context); // downloaded contains your downloaded data. std::cout << std::string(downloaded.begin(), downloaded.end()) << std::endl; + + return 0; } diff --git a/sdk/storage/azure-storage-files-shares/CMakeLists.txt b/sdk/storage/azure-storage-files-shares/CMakeLists.txt index 1da515639f..f687d5af09 100644 --- a/sdk/storage/azure-storage-files-shares/CMakeLists.txt +++ b/sdk/storage/azure-storage-files-shares/CMakeLists.txt @@ -111,11 +111,5 @@ if(BUILD_TESTING) endif() if(BUILD_STORAGE_SAMPLES) - target_sources( - azure-storage-sample - PRIVATE - sample/file_share_getting_started.cpp - ) - - target_link_libraries(azure-storage-sample PRIVATE azure-storage-files-shares) + add_subdirectory(samples) endif() diff --git a/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt b/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt new file mode 100644 index 0000000000..ad0d84c196 --- /dev/null +++ b/sdk/storage/azure-storage-files-shares/samples/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT + +cmake_minimum_required (VERSION 3.13) + +add_executable(file-share-getting-started file_share_getting_started.cpp) +target_link_libraries(file-share-getting-started PRIVATE azure-storage-files-shares) diff --git a/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp b/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp similarity index 62% rename from sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp rename to sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp index 07036d00ed..96a1bb10f3 100644 --- a/sdk/storage/azure-storage-files-shares/sample/file_share_getting_started.cpp +++ b/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp @@ -1,20 +1,39 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; -SAMPLE(FileShareGettingStarted, FileShareGettingStarted) -void FileShareGettingStarted() + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} + +int main() { using namespace Azure::Storage::Files::Shares; - std::string shareName = "sample-share"; - std::string fileName = "sample-file"; - std::string fileContent = "Hello Azure!"; + const std::string shareName = "sample-share"; + const std::string fileName = "sample-file"; + const std::string fileContent = "Hello Azure!"; auto shareClient = ShareClient::CreateFromConnectionString(GetConnectionString(), shareName); shareClient.CreateIfNotExists(); @@ -38,4 +57,6 @@ void FileShareGettingStarted() fileClient.DownloadTo(buffer.data(), buffer.size()); std::cout << std::string(buffer.begin(), buffer.end()) << std::endl; + + return 0; } diff --git a/sdk/storage/azure-storage-queues/CMakeLists.txt b/sdk/storage/azure-storage-queues/CMakeLists.txt index 081d0e42f7..482ccaadf0 100644 --- a/sdk/storage/azure-storage-queues/CMakeLists.txt +++ b/sdk/storage/azure-storage-queues/CMakeLists.txt @@ -98,12 +98,5 @@ if(BUILD_TESTING) endif() if(BUILD_STORAGE_SAMPLES) - target_sources( - azure-storage-sample - PRIVATE - sample/queue_encode_message.cpp - sample/queue_getting_started.cpp - ) - - target_link_libraries(azure-storage-sample PRIVATE azure-storage-queues) + add_subdirectory(samples) endif() diff --git a/sdk/storage/azure-storage-queues/samples/CMakeLists.txt b/sdk/storage/azure-storage-queues/samples/CMakeLists.txt new file mode 100644 index 0000000000..f7a3c35167 --- /dev/null +++ b/sdk/storage/azure-storage-queues/samples/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: MIT + +cmake_minimum_required (VERSION 3.13) + +add_executable(queue-getting-started queue_getting_started.cpp) +target_link_libraries(queue-getting-started PRIVATE azure-storage-queues) + +add_executable(queue-encode-message queue_encode_message.cpp) +target_link_libraries(queue-encode-message PRIVATE azure-storage-queues) diff --git a/sdk/storage/azure-storage-queues/sample/queue_encode_message.cpp b/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp similarity index 66% rename from sdk/storage/azure-storage-queues/sample/queue_encode_message.cpp rename to sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp index dd450bf6fb..3d8f1682a6 100644 --- a/sdk/storage/azure-storage-queues/sample/queue_encode_message.cpp +++ b/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp @@ -1,19 +1,39 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + #include +#include #include +#include #include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; + + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} -SAMPLE(QueuesEncodeMessage, QueuesEncodeMessage) -void QueuesEncodeMessage() +int main() { using namespace Azure::Storage::Queues; - std::string QueueName = "sample-queue"; + + const std::string QueueName = "sample-queue"; auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); queueClient.Create(); @@ -32,4 +52,6 @@ void QueuesEncodeMessage() // message even it's in plaintext. We need to decode the message in that case. auto decodedMessage = Azure::Core::Convert::Base64Decode(receivedMessage.MessageText); assert(decodedMessage == binaryMessage); + + return 0; } diff --git a/sdk/storage/azure-storage-queues/sample/queue_getting_started.cpp b/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp similarity index 79% rename from sdk/storage/azure-storage-queues/sample/queue_getting_started.cpp rename to sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp index 145b2d95ac..0c37e4d650 100644 --- a/sdk/storage/azure-storage-queues/sample/queue_getting_started.cpp +++ b/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp @@ -1,15 +1,35 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // SPDX-License-Identifier: MIT +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include #include +#include #include #include -#include "samples_common.hpp" +std::string GetConnectionString() +{ + const static std::string ConnectionString = ""; + + if (!ConnectionString.empty()) + { + return ConnectionString; + } + const static std::string envConnectionString = std::getenv("AZURE_STORAGE_CONNECTION_STRING"); + if (!envConnectionString.empty()) + { + return envConnectionString; + } + throw std::runtime_error("Cannot find connection string."); +} using namespace Azure::Storage::Queues; -std::string QueueName = "sample-queue"; +const std::string QueueName = "sample-queue"; void ProducerFunc() { @@ -76,8 +96,7 @@ void ConsumerFunc2() } } -SAMPLE(QueuesGettingStarted, QueuesGettingStarted) -void QueuesGettingStarted() +int main() { auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); queueClient.Create(); @@ -85,4 +104,6 @@ void QueuesGettingStarted() ProducerFunc(); ConsumerFunc(); ConsumerFunc2(); + + return 0; }