Add samples (#2342)

This commit is contained in:
JinmingHu 2021-05-26 02:08:17 +08:00 committed by GitHub
parent fcf75f80d1
commit 0876ff4fd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 2 deletions

View File

@ -113,6 +113,8 @@ if(BUILD_STORAGE_SAMPLES)
azure-storage-sample
PRIVATE
sample/blob_getting_started.cpp
sample/blob_list_operation.cpp
sample/blob_sas.cpp
)
target_link_libraries(azure-storage-sample PRIVATE azure-storage-blobs)

View File

@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <iostream>
#include <azure/storage/blobs.hpp>
#include "samples_common.hpp"
SAMPLE(BlobListOperation, BlobListOperation)
void BlobListOperation()
{
using namespace Azure::Storage::Blobs;
std::string containerName = "sample-container";
std::string blobName = "sample-blob";
std::string blobContent = "Hello Azure!";
{
// Create some containers and blobs for test
for (int i = 0; i < 2; ++i)
{
auto containerClient = BlobContainerClient::CreateFromConnectionString(
GetConnectionString(), containerName + std::to_string(i));
containerClient.CreateIfNotExists();
for (int j = 0; j < 3; ++j)
{
BlockBlobClient blobClient
= containerClient.GetBlockBlobClient(blobName + std::to_string(j));
blobClient.UploadFrom(
reinterpret_cast<const uint8_t*>(blobContent.data()), blobContent.size());
}
}
}
auto serviceClient = BlobServiceClient::CreateFromConnectionString(GetConnectionString());
for (auto containerPage = serviceClient.ListBlobContainers(); containerPage.HasPage();
containerPage.MoveToNextPage())
{
for (auto& container : containerPage.BlobContainers)
{
// Below is what you want to do with each container
std::cout << "blob container: " << container.Name << std::endl;
for (auto blobPage = serviceClient.GetBlobContainerClient(container.Name).ListBlobs();
blobPage.HasPage();
blobPage.MoveToNextPage())
{
for (auto& blob : blobPage.Blobs)
{
// Below is what you want to do with each blob
std::cout << " blob: " << blob.Name << std::endl;
}
}
}
}
}

View File

@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <iostream>
#include <azure/storage/blobs.hpp>
#include "samples_common.hpp"
SAMPLE(BlobSas, BlobSas)
void BlobSas()
{
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!";
// Create a container and a blob for test
{
auto credential
= std::make_shared<Azure::Storage::StorageSharedKeyCredential>(accountName, accountKey);
auto containerClient = BlobContainerClient(
"https://" + accountName + ".blob.core.windows.net/" + containerName, credential);
containerClient.CreateIfNotExists();
BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName);
blobClient.UploadFrom(reinterpret_cast<const uint8_t*>(blobContent.data()), blobContent.size());
}
Azure::Storage::Sas::BlobSasBuilder sasBuilder;
sasBuilder.ExpiresOn = std::chrono::system_clock::now() + std::chrono::minutes(60);
sasBuilder.BlobContainerName = containerName;
sasBuilder.BlobName = blobName;
sasBuilder.Resource = Azure::Storage::Sas::BlobSasResource::Blob;
// Read permission only
sasBuilder.SetPermissions(Azure::Storage::Sas::BlobSasPermissions::Read);
std::string sasToken = sasBuilder.GenerateSasToken(
Azure::Storage::StorageSharedKeyCredential(accountName, accountKey));
auto blobClient = BlobClient(
"https://" + accountName + ".blob.core.windows.net/" + containerName + "/" + blobName
+ sasToken);
// We can read the blob
auto properties = blobClient.GetProperties().Value;
try
{
Azure::Storage::Metadata metadata;
// But we cannot write, this will throw
blobClient.SetMetadata(metadata);
// Never reach here
std::abort();
}
catch (Azure::Storage::StorageException&)
{
}
}

View File

@ -77,6 +77,8 @@ namespace Azure { namespace Storage {
struct ConnectionStringParts
{
std::string AccountName;
std::string AccountKey;
Azure::Core::Url BlobServiceUrl;
Azure::Core::Url FileServiceUrl;
Azure::Core::Url QueueServiceUrl;

View File

@ -9,9 +9,11 @@
#include <stdexcept>
#include <string>
#include <azure/storage/common/storage_credential.hpp>
#include "samples_common.hpp"
const std::string& GetConnectionString()
std::string GetConnectionString()
{
const static std::string ConnectionString = "";
@ -27,6 +29,16 @@ const std::string& GetConnectionString()
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)

View File

@ -7,7 +7,9 @@
#include <map>
#include <string>
const std::string& GetConnectionString();
std::string GetConnectionString();
std::string GetAccountName();
std::string GetAccountKey();
class Sample {
public:

View File

@ -53,6 +53,7 @@ namespace Azure { namespace Storage { namespace _internal {
std::string EndpointSuffix
= getWithDefault(connectionStringMap, "EndpointSuffix", "core.windows.net");
std::string accountName = getWithDefault(connectionStringMap, "AccountName");
connectionStringParts.AccountName = accountName;
std::string endpoint = getWithDefault(connectionStringMap, "BlobEndpoint");
if (endpoint.empty() && !accountName.empty())
@ -83,6 +84,7 @@ namespace Azure { namespace Storage { namespace _internal {
connectionStringParts.QueueServiceUrl = Azure::Core::Url(std::move(endpoint));
std::string accountKey = getWithDefault(connectionStringMap, "AccountKey");
connectionStringParts.AccountKey = accountKey;
if (!accountKey.empty())
{
if (accountName.empty())