Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinming-Hu committed Nov 18, 2021
1 parent 3e19cd0 commit 8df0aae
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 23 deletions.
25 changes: 23 additions & 2 deletions sdk/storage/azure-storage-blobs/samples/blob_getting_started.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <cstdio>
#include <stdexcept>

#include <azure/storage/blobs.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.");
}

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

Expand Down
27 changes: 24 additions & 3 deletions sdk/storage/azure-storage-blobs/samples/blob_list_operation.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <cstdio>
#include <stdexcept>

#include <azure/storage/blobs.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.");
}

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!";
Expand All @@ -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)
{
Expand All @@ -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())
Expand Down
44 changes: 37 additions & 7 deletions sdk/storage/azure-storage-blobs/samples/blob_sas.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <cstdio>
#include <stdexcept>

#include <azure/storage/blobs.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()
{
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<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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <cstdio>
#include <stdexcept>

#include <azure/storage/files/datalake.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.");
}

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <cstdio>
#include <stdexcept>

#include <azure/storage/files/shares.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.");
}

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);
Expand Down
25 changes: 23 additions & 2 deletions sdk/storage/azure-storage-queues/samples/queue_encode_message.cpp
Original file line number Diff line number Diff line change
@@ -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 <cassert>
#include <cstdio>
#include <iostream>
#include <stdexcept>

#include <azure/core/base64.hpp>
#include <azure/storage/queues.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.");
}

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.
Expand Down
31 changes: 26 additions & 5 deletions sdk/storage/azure-storage-queues/samples/queue_getting_started.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <iostream>
#include <stdexcept>
#include <thread>

#include <azure/storage/queues.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;
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)
{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -77,7 +98,7 @@ void ConsumerFunc2()

int main()
{
auto queueClient = QueueClient::CreateFromConnectionString(connectionString, QueueName);
auto queueClient = QueueClient::CreateFromConnectionString(GetConnectionString(), QueueName);
queueClient.Create();

ProducerFunc();
Expand Down

0 comments on commit 8df0aae

Please sign in to comment.