question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upload sample with pause/resume

See original GitHub issue

We’re trying to upload a large file and want to support pause/resume. However the upload doesn’t finish and is stuck in the while loop:

TransferManager.Configurations.BlockSize = 4 * 1024 * 1024; // 4 MB

// create stream with 100 MB
var content = Enumerable
    .Range(0, 100 * 1024 * 1024)
    .Select(i => (byte)'b')
    .ToArray();
var source = new MemoryStream(content);

TransferCheckpoint checkPoint = null;
while (true)
{
    Console.Write(".");
    // Resume if there is already a checkpoint
    var context = checkPoint != null
        ? new SingleTransferContext(checkPoint)
        : new SingleTransferContext();

    context.ShouldOverwriteCallback = (src, dest) => true;
    context.LogLevel = Microsoft.WindowsAzure.Storage.LogLevel.Verbose;
    var recorder = new ProgressRecorder();
    context.ProgressHandler = recorder;
    try
    {
        // Interrupt upload after 20 seconds
        using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20)))
        {
            await TransferManager.UploadAsync(source, blob, null, context, cts.Token);
        }
        // break if no exception occured, i.e. the upload finished
        break;
    }
    catch (OperationCanceledException) { }
    checkPoint = context.LastCheckpoint; // save the last checkpoint
}

What are we doing wrong?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dgodwin1175commented, Nov 21, 2017

Once the upload is cancelled, you can save the checkpoint like this:

        try
        {
            return TransferManager.UploadAsync(file.FileName, destinationBlob, options, context, token);
        }
        catch (TaskCanceledException ex)
        {
            checkpoint = context.LastCheckpoint;
            memoryStream = new MemoryStream();
            formatter.Serialize(memoryStream, checkpoint);
            file.Checkpoint = Convert.ToBase64String(memoryStream.ToArray());
            throw ex;
        }

And to resume your upload from the saved checkpoint:

        memoryStream = new MemoryStream(Convert.FromBase64String(file.Checkpoint));
        checkpoint = formatter.Deserialize(memoryStream) as TransferCheckpoint;
        var context = new SingleTransferContext(checkpoint);
        context.SetAttributesCallback = (destination) =>
        {
            var destBlob = destination as CloudBlob;
            destBlob.Properties.ContentType = ContentType;
        };

Then upload the file again with the above context object.

0reactions
haldarsumanacommented, Sep 23, 2021

Is there a way to save context.LastCheckpoint in the database or as a file to use it later to start blob upload from the last checkpoint by using Azure Data Movement library?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pause Resume File Upload using JavaScript
In this tutorial, we are going to see how to implement file upload with pause and resume options using FineUploader.
Read more >
How to Pause Upload, Cancel Upload, Restore Broken ...
To pause the upload, you will simply break the connection. Call Stream.Close() on a stream object returned by IResumableUpload.
Read more >
How to resume a paused or broken file upload
The idea is to provide the user with a button to pause an upload in progress and to resume it again later. Pausing...
Read more >
Pause and resume file upload with dotnet core and javascript
All that has been posted is a program description, but that doesn't tell us what problem you're having. · Does this answer your...
Read more >
Uploading File to Firebase with Pause and ... - YouTube
We will see how to pause and resume a download plus have a ... Upload Images / Files to Firebase In React -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found