Search code examples
c++azure-blob-storagecasablancacpprest-sdk

Cancelling an upload task


I've done some reading regarding the Azure SDK and in order to cancel a task you seemingly need to pass in a cancellation_token.

My upload code is very simple:

azure::storage::cloud_block_blob blockBlob = container.get_block_blob_reference(fileLeaf.wstring());

auto task = blockBlob.upload_from_file_async(fullFilePath);

However, some files I upload are potentially very large, and I would like to be able to cancel this operation. I'll probably also likely use continuations and would need all those cancelling too, if that's possible.

The problem I'm having is I can't see any way of attaching a cancellation_token to the task.

Any pointers?


Solution

  • There is a sample code using PPL library, I refered to it and changed the code for canceling task using PPLX library within C++ REST SDK which be used for Azure Storage SDK for C++, please try the code below.

    /* Declare a cancellation_token_source and get the cancellation_token, 
     * please see http://microsoft.github.io/cpprestsdk/classpplx_1_1cancellation__token__source.html
    */
    #include <pplxtasks.h>
    cancellation_token_source cts;
    auto token = cts.get_token();
    
    //Pass the cancellation_toke to task via then method, please see https://msdn.microsoft.com/en-us/library/jj987787.aspx
    task.then([]{}, token).wait();
    
    // Cancel the task
    cts.cancel();
    

    Hope it helps.