none
Azure Storage Client Library for C++ upload blob with *async methods

    Question

  • Can you give an example with *async methods ? For example - upload_from_stream_async
    Because is not clear how to upload blob and show progress.
    Thanks.
    Monday, July 13, 2015 8:44 AM

Answers

  • Hi Kisliak,

    Usage of async methods in Azure Storage C++ client library is very similar to its sync version counterpart. Below is a sample for blob upload (error handling logic ignored for simplicity):

        // Initialize storage account
        azure::storage::cloud_storage_account
     storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);
         
        // Create a blob container
        azure::storage::cloud_blob_client
     blob_client = storage_account.create_cloud_blob_client();
        azure::storage::cloud_blob_container
     container = blob_client.get_container_reference(U("my-sample-container"));     
    
        container.create_if_not_exists();
    
        // Upload a blob from a file asynchronously
        concurrency::streams::istream
     input_stream = concurrency::streams::file_stream<uint8_t>::open_istream(U("DataFile.txt")).get();
        azure::storage::cloud_block_blob
     blob1 = container.get_block_blob_reference(U("my-blob-1"));
        pplx::task<void>
     upload_task = blob1.upload_from_stream_async(input_stream);

          At this point, blob upload operation is running asynchronously in a different thread. Depending on the work you want to do, you may choose to:

          1. Wait for the upload operation to complete, and then continue.

        // wait for the upload operation to complete
        upload_task.wait();
    
        // then conitnue your work here.

          2. continue your work asynchronously by posting a continuation function to the returned task. When the upload operation is completed, your work will be scheduled automatically. For example,

         upload_task.then(
            []() -> void {            
                // continue your work here asynchronously after the upload operation is completed
            });

        However, the async methods do not have built-in progress reporting support.

    Thanks,

    Vincent


    Friday, July 17, 2015 1:20 AM

All replies

  • Hi,

     Couldn't find an example using the C++ storage libraries, however refer the following articles/discussions for ideas on how it can be achieved.
     http://stackoverflow.com/questions/9971082/track-the-upload-progress-while-uploading-file-to-azure-blob
     http://blogs.msdn.com/b/kwill/archive/2013/03/06/asynchronous-parallel-block-blob-transfers-with-progress-change-notification-2-0.aspx
     
    Regards,
    Nithin Rathnakar

    Monday, July 13, 2015 6:55 PM
  • Hi, actually it does not help me.
    Because I use Azure Storage Client Library for C++ 
    https://github.com/Azure/azure-storage-cpp
    based on on the C++ REST SDK "Casablanca"

    And I didn't find examples how to use it using async calls.
    Wednesday, July 15, 2015 11:09 AM
  • Hi Kisliak,

    Usage of async methods in Azure Storage C++ client library is very similar to its sync version counterpart. Below is a sample for blob upload (error handling logic ignored for simplicity):

        // Initialize storage account
        azure::storage::cloud_storage_account
     storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);
         
        // Create a blob container
        azure::storage::cloud_blob_client
     blob_client = storage_account.create_cloud_blob_client();
        azure::storage::cloud_blob_container
     container = blob_client.get_container_reference(U("my-sample-container"));     
    
        container.create_if_not_exists();
    
        // Upload a blob from a file asynchronously
        concurrency::streams::istream
     input_stream = concurrency::streams::file_stream<uint8_t>::open_istream(U("DataFile.txt")).get();
        azure::storage::cloud_block_blob
     blob1 = container.get_block_blob_reference(U("my-blob-1"));
        pplx::task<void>
     upload_task = blob1.upload_from_stream_async(input_stream);

          At this point, blob upload operation is running asynchronously in a different thread. Depending on the work you want to do, you may choose to:

          1. Wait for the upload operation to complete, and then continue.

        // wait for the upload operation to complete
        upload_task.wait();
    
        // then conitnue your work here.

          2. continue your work asynchronously by posting a continuation function to the returned task. When the upload operation is completed, your work will be scheduled automatically. For example,

         upload_task.then(
            []() -> void {            
                // continue your work here asynchronously after the upload operation is completed
            });

        However, the async methods do not have built-in progress reporting support.

    Thanks,

    Vincent


    Friday, July 17, 2015 1:20 AM
  • Hi Vincent, thanks,
    >>async methods do not have built-in progress reporting support.
    This was the point of my question. I already implemented exactly the same functionality as you showed me, but honestly, really strange that is not possible to get progress when we need upload big blobs.
    Sunday, July 19, 2015 7:38 AM