From 2f95fb0af3a7dd42eb496d82a18789bd78853d73 Mon Sep 17 00:00:00 2001 From: Jinming Hu Date: Tue, 3 Mar 2020 15:23:30 +0800 Subject: [PATCH] Add sample for blob_client --- sample/Makefile | 3 - sample/sample.cpp | 143 +++++++++++++-------------------------------- sample/sample2.cpp | 118 +++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 104 deletions(-) delete mode 100644 sample/Makefile create mode 100644 sample/sample2.cpp diff --git a/sample/Makefile b/sample/Makefile deleted file mode 100644 index 81a001d..0000000 --- a/sample/Makefile +++ /dev/null @@ -1,3 +0,0 @@ - -all: - g++ --std=c++11 sample.cpp -o run -I../include -L.. -lazure-storage -lcurl diff --git a/sample/sample.cpp b/sample/sample.cpp index 84015db..aa851ba 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -1,118 +1,59 @@ - -#include -#include -#include -#include - #include "storage_credential.h" #include "storage_account.h" #include "blob/blob_client.h" -static std::string account_name = "YOUR_ACCOUNT_NAME"; - -// Provide either account key or access token -// Account key operations require RBAC 'Storage Blob Data Owner' -static std::string account_key = ""; // Storage account key if using shared key auth -static std::string access_token = ""; // Get an access token via `az account get-access-token --resource https://storage.azure.com/ -o tsv --query accessToken` - -using namespace azure::storage_lite; - -void checkstatus() -{ - if(errno == 0) - { - printf("Success\n"); - } - else - { - printf("Fail\n"); - } -} +#include +#include +#include int main() { - std::shared_ptr cred = nullptr; - if (!access_token.empty()) - { - cred = std::make_shared(access_token); - } - else - { - cred = std::make_shared(account_name, account_key); - } + using namespace azure::storage_lite; + + std::string account_name = "YOUR_STORAGE_ACCOUNT"; + std::string account_key = ""; + std::string container_name = "my-sample-container"; + std::string blob_name = "my-sample-blob"; + + // Create a file for uploading later + std::string sample_file = "sample-file"; + std::ofstream fout("sample-file"); + fout << "Hello world!\n"; + fout.close(); + + std::shared_ptr cred = std::make_shared(account_name, account_key); std::shared_ptr account = std::make_shared(account_name, cred, /* use_https */ true); - auto bC = std::make_shared(account, 10); - //auto f1 = bc.list_containers(""); - //f1.wait(); - // - std::string containerName = "jasontest1"; - std::string blobName = "test.txt"; - std::string destContainerName = "jasontest1"; - std::string destBlobName = "test.txt.copy"; - std::string uploadFileName = "test.txt"; - std::string downloadFileName = "download.txt"; + blob_client client(account, 16); - bool exists = true; - blob_client_wrapper bc(bC); - - exists = bc.container_exists(containerName); - - if(!exists) + auto ret = client.create_container(container_name).get(); + if (!ret.success()) { - bc.create_container(containerName); - assert(errno == 0); + std::cout << "Failed to create container, Error: " << ret.error().code << ", " << ret.error().code_name << std::endl; } - assert(errno == 0); - exists = bc.blob_exists(containerName, "testsss.txt"); - assert(errno == 0); - assert(!exists); - std::cout <<"Start upload Blob: " << blobName << std::endl; - bc.upload_file_to_blob(uploadFileName, containerName, blobName); - std::cout <<"Error upload Blob: " << errno << std::endl; - assert(errno == 0); - - exists = bc.blob_exists(containerName, blobName); - assert(errno == 0); - assert(exists); - - auto blobProperty = bc.get_blob_property(containerName, blobName); - assert(errno == 0); - std::cout <<"Size of BLob: " << blobProperty.size << std::endl; - - auto blobs = bc.list_blobs_segmented(containerName, "/", "", ""); - std::cout <<"Size of BLobs: " << blobs.blobs.size() << std::endl; - std::cout <<"Error Size of BLobs: " << errno << std::endl; - assert(errno == 0); - - time_t last_modified; - bc.download_blob_to_file(containerName, blobName, downloadFileName, last_modified); - std::cout <<"Download Blob done: " << errno << std::endl; - assert(errno == 0); - - exists = bc.container_exists(destContainerName); - - if(!exists) + std::ifstream fin(sample_file, std::ios_base::in | std::ios_base::binary); + std::vector> metadata; + metadata.emplace_back(std::make_pair("meta_key1", "meta-value1")); + metadata.emplace_back(std::make_pair("meta_key2", "meta-value2")); + ret = client.upload_block_blob_from_stream(container_name, blob_name, fin, metadata).get(); + if (!ret.success()) { - bc.create_container(destContainerName); - assert(errno == 0); + std::cout << "Failed to upload blob, Error: " << ret.error().code << ", " << ret.error().code_name << std::endl; + } + fin.close(); + + std::ostringstream out_stream; + ret = client.download_blob_to_stream(container_name, blob_name, 0, 0, out_stream).get(); + if (!ret.success()) + { + std::cout << "Failed to download blob, Error: " << ret.error().code << ", " << ret.error().code_name << std::endl; + } + else + { + std::cout << out_stream.str(); } - // copy blob - bc.start_copy(containerName, blobName, destContainerName, destBlobName); - auto property = bc.get_blob_property(destContainerName, destBlobName); - std::cout << "Copy status: " << property.copy_status < +#include +#include +#include + +#include "storage_credential.h" +#include "storage_account.h" +#include "blob/blob_client.h" + +static std::string account_name = "YOUR_ACCOUNT_NAME"; + +// Provide either account key or access token +// Account key operations require RBAC 'Storage Blob Data Owner' +static std::string account_key = ""; // Storage account key if using shared key auth +static std::string access_token = ""; // Get an access token via `az account get-access-token --resource https://storage.azure.com/ -o tsv --query accessToken` + +using namespace azure::storage_lite; + +void checkstatus() +{ + if(errno == 0) + { + printf("Success\n"); + } + else + { + printf("Fail\n"); + } +} + +int main() +{ + std::shared_ptr cred = nullptr; + if (!access_token.empty()) + { + cred = std::make_shared(access_token); + } + else + { + cred = std::make_shared(account_name, account_key); + } + std::shared_ptr account = std::make_shared(account_name, cred, /* use_https */ true); + auto bC = std::make_shared(account, 10); + //auto f1 = bc.list_containers(""); + //f1.wait(); + // + std::string containerName = "jasontest1"; + std::string blobName = "test.txt"; + std::string destContainerName = "jasontest1"; + std::string destBlobName = "test.txt.copy"; + std::string uploadFileName = "test.txt"; + std::string downloadFileName = "download.txt"; + + bool exists = true; + blob_client_wrapper bc(bC); + + exists = bc.container_exists(containerName); + + if(!exists) + { + bc.create_container(containerName); + assert(errno == 0); + } + + assert(errno == 0); + exists = bc.blob_exists(containerName, "testsss.txt"); + assert(errno == 0); + assert(!exists); + std::cout <<"Start upload Blob: " << blobName << std::endl; + bc.upload_file_to_blob(uploadFileName, containerName, blobName); + std::cout <<"Error upload Blob: " << errno << std::endl; + assert(errno == 0); + + exists = bc.blob_exists(containerName, blobName); + assert(errno == 0); + assert(exists); + + auto blobProperty = bc.get_blob_property(containerName, blobName); + assert(errno == 0); + std::cout <<"Size of BLob: " << blobProperty.size << std::endl; + + auto blobs = bc.list_blobs_segmented(containerName, "/", "", ""); + std::cout <<"Size of BLobs: " << blobs.blobs.size() << std::endl; + std::cout <<"Error Size of BLobs: " << errno << std::endl; + assert(errno == 0); + + time_t last_modified; + bc.download_blob_to_file(containerName, blobName, downloadFileName, last_modified); + std::cout <<"Download Blob done: " << errno << std::endl; + assert(errno == 0); + + exists = bc.container_exists(destContainerName); + + if(!exists) + { + bc.create_container(destContainerName); + assert(errno == 0); + } + + // copy blob + bc.start_copy(containerName, blobName, destContainerName, destBlobName); + auto property = bc.get_blob_property(destContainerName, destBlobName); + std::cout << "Copy status: " << property.copy_status <