提高Blob上传性能

时间:2019-04-11 09:41:52

标签: c++ azure-storage

我正在将c ++应用程序与Azure blob存储集成在一起。 为此,我围绕wastorage 4.0 API实现了包装器类。

#include "stdafx.h"
#include "AzureStorage.h"
// Microsoft Azure Library Header Includes.
#include "was\storage_account.h"
#include "was\blob.h"

struct RadAzureData{
    azure::storage::cloud_storage_account storage_account;
    azure::storage::cloud_blob_client blob_client;
    azure::storage::cloud_blob_container container;
};

   RadAzureStorage::RadAzureStorage():
   RadCloudStorageInterface(RAD_STORAGE_TYPE::AZURE_CLOUD)
   {
   }

   RadAzureStorage::RadAzureStorage(std::string accountName1, std::string     accountKey1, std::string containerName1) :        RadCloudStorageInterface(RAD_STORAGE_TYPE::AZURE_CLOUD)
   {
       std::wstring accountNameWS(accountName1.begin(), accountName1.end());
       std::wstring accountKeyWS(accountKey1.begin(), accountKey1.end());
       std::wstring containerNameWS(containerName1.begin(), containerName1.end());
       d = new RadAzureData();
       accountName = accountNameWS;
       accountKey = accountKeyWS;
       containerName = containerNameWS;
       std::wstring connStr1 = L"AccountName=" + accountName + L";AccountKey=" + accountKey + L";DefaultEndpointsProtocol=https";
       d->storage_account =        azure::storage::cloud_storage_account::parse(connStr1.c_str());
       // Create a blob container
       d->blob_client = d->storage_account.create_cloud_blob_client();
       d->container = d->blob_client.get_container_reference(containerName.c_str());
       CreateContainer();
   }


   bool RadAzureStorage::CreateContainer()
   {
       try
       {
           d->container.create_if_not_exists();
       }

       catch (const azure::storage::storage_exception& e)
       {
          cout<<"Exception in container creation: " << e.what()<<endl;
          cout <<"The request that started at:" << e.result().start_time().to_string().c_str() << " and ended at " << e.result().end_time().to_string().c_str() << " resulted in HTTP status code " << e.result().http_status_code() << " and the request ID reported by the server was " << e.result().service_request_id().c_str()<<endl;
          return false;
       }
    return true;
   }

   bool RadAzureStorage::UploadFile(std::string blockBlobName, std::string dicomFileLocation)
   {
       std::wstring blockBlobNameWS(blockBlobName.begin(), blockBlobName.end());
       std::wstring dicomFileLocationWS(dicomFileLocation.begin(), dicomFileLocation.end());
       // Create a Block Blob Object.
       azure::storage::cloud_block_blob block_blob = d->container.get_block_blob_reference(blockBlobNameWS.c_str());
       // Upload Block Blob to container.
       try
       {
           block_blob.upload_from_file(dicomFileLocationWS.c_str());
       }
       catch (const azure::storage::storage_exception& e)
       {
           cout<< "Exception in file upload: " << e.what() << endl;
           cout<< "The request that started at:" << e.result().start_time().to_string().c_str() << " and ended at " << e.result().end_time().to_string().c_str() << " resulted in HTTP status code " << e.result().http_status_code() << " and the request ID reported by the server was " << e.result().service_request_id().c_str() << endl;
           return false;
       }

       return true;
   }
   #undef __FILENAME__

从应用程序中实例化RadAzureStorage类并调用UploadFile API。

   RadAzureStorage* clsi = new RadAzureStorage(accountname, acesskey,        containername);
   <<timer.start>>
   clsi->UploadFile(blockBlobName, file);
   <<timer.end>>
   cout << timer.ellapsedMilliSeconds<< "ms"<< endl;

UploadFile API的文件大小介于190-250 KB之间需要14-16毫秒。

是否有一些关于浪费初始化的参数可以修改,以在10毫秒内实现。

当前的测试环境在Azure南印度托管: 1. VM:Windows Server 2016、4v Core,16 GB内存。 2.热层访问,存储帐户。

请注意:在C#上实现了类似的逻辑,对于同一数据集,每个文件的上传均在10毫秒内完成。

1 个答案:

答案 0 :(得分:0)

此延迟也可能是由使用的存储帐户类型引起的。使用高级存储帐户时,与GPV1相比,您可以获得更高的性能。

还有一个新产品仍在Azure Premium blobs中。高级存储帐户存储在SSD与HDD上,因此使用GPV2高级版时,您也可以获得更好的性能。