Azure / azure-storage-cpp

Microsoft Azure Storage Client Library for C++
http://azure.github.io/azure-storage-cpp
Apache License 2.0
132 stars 147 forks source link

Unable to specify default_request_options when creating cloud_blob_container from URI #324

Open mattdurak opened 4 years ago

mattdurak commented 4 years ago

If I have code like the following, then I can create a container and it will use the default request options I specify:

Listing A:

auto account = azure::storage::cloud_storage_account::parse(L"connection_string_here");
auto client = account.create_cloud_blob_client(my_generate_default_options());
auto container = client.get_container_reference(my_container_name);

Now container will use the m_client member to get the default options for requests and everything works nicely.

If instead, I want to create a container from a URI and credentials, I have the nice helper function available in the SDK:

Listing B:

azure::storage::storage_credentials credentials{ "my_sas_token_string" };
azure::storage::cloud_blob_container container("https://my-container-uri", credentials);

Internally, this creates a client for the container like this:

    void cloud_blob_container::init(storage_credentials credentials)
    {
        utility::string_t snapshot;
        m_uri = core::verify_blob_uri(m_uri, credentials, snapshot);

        if (!core::parse_container_uri(m_uri, m_name))
        {
            throw std::invalid_argument("uri");
        }

        m_client = cloud_blob_client(core::get_service_client_uri(m_uri), std::move(credentials));
    }

This means, the client cannot have default request options. Instead, I have to write more code to do the same thing init is doing if I want to have the default options but create from a URI and SAS token:

Listing C:

azure::storage::storage_credentials credentials{ "my_sas_token_string" };
utility::string_t snapshot;
azure::storage::storage_uri my_uri = azure::storage::core::verify_blob_uri("https://my-container-uri", credentials, snapshot);
utility::string_t container_name;
if (!azure::storage::core::parse_container_uri(my_uri, container_name))
{
    throw std::invalid_argument("uri");
}
azure::storage::cloud_blob_client client(azure::storage::core::get_service_client_uri(my_uri), std::move(credentials), my_generate_default_options());
azure::storage::cloud_blob_container container(container_name, client);

What I would love is to just write this (instead of the code in Listing C)

Listing D:

azure::storage::storage_credentials credentials{ "my_sas_token_string" };
azure::storage::cloud_blob_container container("https://my-container-uri", credentials, my_generate_default_options());

This lets us create the container with either a connection string or a SAS token, and then act on the container and blobs using a retry policy we specify, without having to give the request options for every operation on the blob.

Jinming-Hu commented 4 years ago

@mattdurak Maybe in the future we'll consider add another constructor for cloud_blob_client which accepts default blob options as third parameter. Thanks for your feedback.