restructure storage samples (#3113)

* restructure storage samples

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Victor Vazquez <victor.vazquez@microsoft.com>

* u

* u2

* fix link

* clang-format

Co-authored-by: Victor Vazquez <victor.vazquez@microsoft.com>
This commit is contained in:
JinmingHu 2021-11-19 09:59:34 +08:00 committed by GitHub
parent a831b4b3f6
commit 1b814a8757
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 273 additions and 222 deletions

View File

@ -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)

View File

@ -131,9 +131,9 @@ target_link_libraries(<your project name> 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)
<!-- LINKS -->
[azsdk_vcpkg_install]: https://github.com/Azure/azure-sdk-for-cpp#download--install-the-sdk

View File

@ -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()

View File

@ -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)

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/storage/blobs.hpp>
#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;
}

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/storage/blobs.hpp>
#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;
}

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/storage/blobs.hpp>
#include "samples_common.hpp"
std::string GetConnectionString()
{
const static std::string ConnectionString = "";
SAMPLE(BlobSas, BlobSas)
void BlobSas()
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()
{
using namespace Azure::Storage::Blobs;
std::string accountName = GetAccountName();
std::string accountKey = GetAccountKey();
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<Azure::Storage::StorageSharedKeyCredential>(accountName, accountKey);
auto credential = std::make_shared<Azure::Storage::StorageSharedKeyCredential>(
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<const uint8_t*>(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

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/storage/blobs.hpp>
#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;
}

View File

@ -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()

View File

@ -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 <cstdio>
#include <stdexcept>
#include <string>
#include <azure/storage/common/storage_credential.hpp>
#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 <sample name>\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;
}

View File

@ -1,42 +0,0 @@
// Copyright(c) Microsoft Corporation.All rights reserved.
// SPDX - License - Identifier : MIT
#pragma once
#include <functional>
#include <map>
#include <string>
std::string GetConnectionString();
std::string GetAccountName();
std::string GetAccountKey();
class Sample {
public:
virtual ~Sample() = default;
static const std::map<std::string, std::function<void()>>& samples() { return m_samples(); }
protected:
static void add_sample(std::string sample_name, std::function<void()> func)
{
m_samples().emplace(std::move(sample_name), std::move(func));
}
private:
static std::map<std::string, std::function<void()>>& m_samples()
{
static std::map<std::string, std::function<void()>> 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_; \
}

View File

@ -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()

View File

@ -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)

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/storage/files/datalake.hpp>
#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<uint8_t> downloaded = result.Value.Body->ReadToEnd(context);
// downloaded contains your downloaded data.
std::cout << std::string(downloaded.begin(), downloaded.end()) << std::endl;
return 0;
}

View File

@ -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()

View File

@ -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)

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/storage/files/shares.hpp>
#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;
}

View File

@ -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()

View File

@ -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)

View File

@ -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 <cassert>
#include <cstdio>
#include <iostream>
#include <stdexcept>
#include <azure/core/base64.hpp>
#include <azure/storage/queues.hpp>
#include "samples_common.hpp"
std::string GetConnectionString()
{
const static std::string ConnectionString = "";
SAMPLE(QueuesEncodeMessage, QueuesEncodeMessage)
void QueuesEncodeMessage()
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::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;
}

View File

@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <thread>
#include <azure/storage/queues.hpp>
#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;
}