使用cpprest SDK访问Azure存储服务

时间:2018-02-08 20:45:54

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

我正在尝试使用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.有人可以告诉我,如果我这样做了吗?

1 个答案:

答案 0 :(得分:0)

请注意,您没有以正确的方式使用cpprest-sdk,因为您在上面的代码中所做的是尝试直接(并且错误地)调用Azure存储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++