【问题标题】:Access Azure storage service using cpprest SDK使用 cpprest SDK 访问 Azure 存储服务
【发布时间】:2018-07-19 13:20:09
【问题描述】:

我正在尝试使用 cpprest sdk 列出我的 Azure 存储帐户中的 blob,这是我的代码:

pplx::task<void> HTTPRequestCustomHeadersAsync()
{
    http_client client(L"https://<account-name>.blob.core.windows.net/?comp=list");

    // Manually build up an HTTP request with header and request URI.
    http_request request(methods::GET);
    request.headers().add(L"Authorization", L"Sharedkey <account-name>:<account-key>");
    request.headers().add(L"x-ms-date", L"Thu, 08 Feb 2018 20:31:55 GMT ");
    request.headers().add(L"x-ms-version", L"2017-07-29");

    return client.request(request).then([](http_response response)
    {
        // Print the status code.
        std::wostringstream ss;
        ss << L"Server returned returned status code " << response.status_code() << L"." << std::endl;
        std::wcout << ss.str();
    });

    /* Sample output:
    Server returned returned status code 200.
    */
}

我一直收到返回的状态码为 403。如果我做得对,有人可以告诉我吗?

【问题讨论】:

    标签: rest azure azure-storage azure-blob-storage cpprest-sdk


    【解决方案1】:

    请注意,您没有以正确的方式使用 cpprest-sdk,因为您在上面的代码中所做的是试图直接(并且错误地)调用 Azure Storage REST API,而根本不通过 cpprest-sdk。

    实际上,Azure 存储 REST API 合同的 HTTP 标头中的帐户密钥不是纯文本。相反,它是通过Authentication for the Azure Storage Services 中提到的复杂步骤计算的,出于一系列安全考虑。幸运的是,所有这些逻辑都被 cpprest-sdk 封装了,你不需要了解它内部是如何工作的:

    // Define the connection-string with your values.
    const utility::string_t storage_connection_string(U("DefaultEndpointsProtocol=https;AccountName=your_storage_account;AccountKey=your_storage_account_key"));
    
    // Create the blob client.
    azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client();  
    

    我建议你在使用 cpprest-sdk 之前先阅读How to use Blob Storage from C++

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2016-08-23
      • 2017-12-21
      • 2017-06-08
      • 2018-01-10
      • 1970-01-01
      相关资源
      最近更新 更多