代码之家  ›  专栏  ›  技术社区  ›  dodo42

使用cpprest SDK访问Azure存储服务

  •  0
  • dodo42  · 技术社区  · 7 年前

    我正在尝试使用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 回复  |  直到 7 年前
        1
  •  0
  •   Zhaoxing Lu    7 年前

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

    实际上,Azure Storage 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();  
    

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

    推荐文章