azure-sdk-for-cpp/sdk/storage/sample/blob_getting_started.cpp
JinmingHu 069e359562
Rename return types and option types (#449)
* rename return types and option types

* define ConcurrentUploadBlockBlobFromBufferOptions and ConcurrentUploadBlockBlobFromFileOptions

* use std::move

* Rename DownloadTo

* update doc

* Resolved datalake build issues.

* Resolved some cmake issue when linking

Co-authored-by: Tank Tang <kat@microsoft.com>
2020-08-14 13:02:10 +08:00

49 lines
1.4 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include "blobs/blob.hpp"
#include "samples_common.hpp"
#include <iostream>
SAMPLE(BlobsGettingStarted, BlobsGettingStarted)
void BlobsGettingStarted()
{
using namespace Azure::Storage::Blobs;
std::string containerName = "sample-container";
std::string blobName = "sample-blob";
std::string blobContent = "Hello Azure!";
auto containerClient
= BlobContainerClient::CreateFromConnectionString(GetConnectionString(), containerName);
try
{
containerClient.Create();
}
catch (std::runtime_error& e)
{
// The container may already exist
std::cout << e.what() << std::endl;
}
BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName);
blobClient.UploadFromBuffer(
reinterpret_cast<const uint8_t*>(blobContent.data()), blobContent.size());
std::map<std::string, std::string> blobMetadata = {{"key1", "value1"}, {"key2", "value2"}};
blobClient.SetMetadata(blobMetadata);
auto properties = *blobClient.GetProperties();
for (auto metadata : properties.Metadata)
{
std::cout << metadata.first << ":" << metadata.second << std::endl;
}
blobContent.resize(static_cast<std::size_t>(properties.ContentLength));
blobClient.DownloadTo(reinterpret_cast<uint8_t*>(&blobContent[0]), blobContent.size());
std::cout << blobContent << std::endl;
}