Skip to content

Commit

Permalink
restructure storage samples (#3113)
Browse files Browse the repository at this point in the history
* restructure storage samples

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Victor Vazquez <[email protected]>

* u

* u2

* fix link

* clang-format

Co-authored-by: Victor Vazquez <[email protected]>
  • Loading branch information
Jinming-Hu and vhvb1989 authored Nov 19, 2021
1 parent a831b4b commit 1b814a8
Show file tree
Hide file tree
Showing 21 changed files with 273 additions and 222 deletions.
4 changes: 0 additions & 4 deletions sdk/storage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions sdk/storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 2 additions & 11 deletions sdk/storage/azure-storage-blobs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 16 additions & 0 deletions sdk/storage/azure-storage-blobs/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -40,4 +59,6 @@ void BlobsGettingStarted()
blobClient.DownloadTo(buffer.data(), buffer.size());

std::cout << std::string(buffer.begin(), buffer.end()) << std::endl;

return 0;
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -54,4 +73,6 @@ void BlobListOperation()
}
}
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -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 = "";

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<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());
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -51,4 +70,6 @@ void TransactionalChecksum()
{
std::cout << "CRC-64 match" << std::endl;
}

return 0;
}
11 changes: 0 additions & 11 deletions sdk/storage/azure-storage-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
78 changes: 0 additions & 78 deletions sdk/storage/azure-storage-common/sample/main.cpp

This file was deleted.

Loading

0 comments on commit 1b814a8

Please sign in to comment.