From 8df0aaeec437d894e984d339d67f4f3510930e7d Mon Sep 17 00:00:00 2001 From: Jinming Hu Date: Thu, 18 Nov 2021 11:06:11 +0800 Subject: [PATCH] u --- .../samples/blob_getting_started.cpp | 25 ++++++++++- .../samples/blob_list_operation.cpp | 27 ++++++++++-- .../azure-storage-blobs/samples/blob_sas.cpp | 44 ++++++++++++++++--- .../samples/datalake_getting_started.cpp | 25 ++++++++++- .../samples/file_share_getting_started.cpp | 25 ++++++++++- .../samples/queue_encode_message.cpp | 25 ++++++++++- .../samples/queue_getting_started.cpp | 31 ++++++++++--- 7 files changed, 179 insertions(+), 23 deletions(-) diff --git a/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp b/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp index f0558f638d..94902a6697 100644 --- a/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp @@ -1,21 +1,42 @@ // 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 +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."); +} + int main() { using namespace Azure::Storage::Blobs; - const std::string connectionString = ""; const std::string containerName = "sample-container"; const std::string blobName = "sample-blob"; const std::string blobContent = "Hello Azure!"; auto containerClient - = BlobContainerClient::CreateFromConnectionString(connectionString, containerName); + = BlobContainerClient::CreateFromConnectionString(GetConnectionString(), containerName); containerClient.CreateIfNotExists(); diff --git a/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp b/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp index 9d4d04b720..7e2d8a3865 100644 --- a/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp @@ -1,15 +1,36 @@ // 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 +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."); +} + int main() { using namespace Azure::Storage::Blobs; - const std::string connectionString = ""; const std::string containerName = "sample-container"; const std::string blobName = "sample-blob"; const std::string blobContent = "Hello Azure!"; @@ -19,7 +40,7 @@ int main() for (int i = 0; i < 2; ++i) { auto containerClient = BlobContainerClient::CreateFromConnectionString( - connectionString, containerName + std::to_string(i)); + GetConnectionString(), containerName + std::to_string(i)); containerClient.CreateIfNotExists(); for (int j = 0; j < 3; ++j) { @@ -31,7 +52,7 @@ int main() } } - auto serviceClient = BlobServiceClient::CreateFromConnectionString(connectionString); + auto serviceClient = BlobServiceClient::CreateFromConnectionString(GetConnectionString()); for (auto containerPage = serviceClient.ListBlobContainers(); containerPage.HasPage(); containerPage.MoveToNextPage()) diff --git a/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp b/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp index 49469ed976..a7302a3e2c 100644 --- a/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp +++ b/sdk/storage/azure-storage-blobs/samples/blob_sas.cpp @@ -1,26 +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 +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() { using namespace Azure::Storage::Blobs; - const std::string accountName = ""; - const std::string accountKey = ""; 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()); @@ -35,10 +65,10 @@ int main() 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-files-datalake/samples/datalake_getting_started.cpp b/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp index c3b33dd331..501be2285f 100644 --- a/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp +++ b/sdk/storage/azure-storage-files-datalake/samples/datalake_getting_started.cpp @@ -1,21 +1,42 @@ // 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 +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."); +} + int main() { using namespace Azure::Storage::Files::DataLake; - const std::string connectionString = ""; const std::string fileSystemName = "sample-file-system"; const std::string directoryName = "sample-directory"; const std::string fileName = "sample-file"; auto fileSystemClient - = DataLakeFileSystemClient::CreateFromConnectionString(connectionString, fileSystemName); + = DataLakeFileSystemClient::CreateFromConnectionString(GetConnectionString(), fileSystemName); fileSystemClient.CreateIfNotExists(); // Create a directory. diff --git a/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp b/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp index c9e5e060a7..0145855f7d 100644 --- a/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp +++ b/sdk/storage/azure-storage-files-shares/samples/file_share_getting_started.cpp @@ -1,20 +1,41 @@ // 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 +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."); +} + int main() { using namespace Azure::Storage::Files::Shares; - const std::string connectionString = ""; const std::string shareName = "sample-share"; const std::string fileName = "sample-file"; const std::string fileContent = "Hello Azure!"; - auto shareClient = ShareClient::CreateFromConnectionString(connectionString, shareName); + auto shareClient = ShareClient::CreateFromConnectionString(GetConnectionString(), shareName); shareClient.CreateIfNotExists(); ShareFileClient fileClient = shareClient.GetRootDirectoryClient().GetFileClient(fileName); diff --git a/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp b/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp index 8728b874ac..3d8f1682a6 100644 --- a/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp +++ b/sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp @@ -1,20 +1,41 @@ // 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 +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."); +} + int main() { using namespace Azure::Storage::Queues; - const std::string connectionString = ""; const std::string QueueName = "sample-queue"; - auto queueClient = QueueClient::CreateFromConnectionString(connectionString, QueueName); + auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); queueClient.Create(); // Binary message cannot be enqueued directly, we encode the message with Base64. diff --git a/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp b/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp index 7450bdab1f..0c37e4d650 100644 --- a/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp +++ b/sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp @@ -1,18 +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 +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; -const std::string connectionString = ""; const std::string QueueName = "sample-queue"; void ProducerFunc() { - auto queueClient = QueueClient::CreateFromConnectionString(connectionString, QueueName); + auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); for (int i = 0; i < 5; ++i) { @@ -32,7 +53,7 @@ void ProducerFunc() void ConsumerFunc() { - auto queueClient = QueueClient::CreateFromConnectionString(connectionString, QueueName); + auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); int counter = 0; while (counter < 5) @@ -53,7 +74,7 @@ void ConsumerFunc() void ConsumerFunc2() { - auto queueClient = QueueClient::CreateFromConnectionString(connectionString, QueueName); + auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); int counter = 0; while (counter < 5) @@ -77,7 +98,7 @@ void ConsumerFunc2() int main() { - auto queueClient = QueueClient::CreateFromConnectionString(connectionString, QueueName); + auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName); queueClient.Create(); ProducerFunc();