jlo-gmail
jlo-gmail

Reputation: 5048

Uploading large blobs to azure using azure-sdk-for-net is timing out

I am attempting to use c# and the Azure.Storage.Blobs.Specialized.BlockBlobClient to upload large files. I am encountering time-outs. I need a sample that shows how to upload large files (approx 300MB). I have not been able to find a good example.

using Azure.Storage.Blobs.Specialized;
...
using (var fs = fileInfo.Open(FileMode.Open,FileAccess.Read))
{
     var blockBlobClient = new BlockBlobClient(
          connectionString,
         "ore", 
         fileInfo.Name);

     await blockBlobClient.UploadAsync(fs);
}

Upvotes: 1

Views: 1346

Answers (1)

jlo-gmail
jlo-gmail

Reputation: 5048

I had to adjust the network timeout.

var uploadManager = new UploadManager();
await uploadManager.UploadBlob(connectionString, container, fileInfo);

This class handles large uploads, increases the network timeout and provides a Progress output.

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uploader
{
    public class UploadManager
    {
        System.Collections.Concurrent.ConcurrentBag<long> progressBag = null;
        Progress<long> progressHandler = null;

        public UploadManager()
        {
            progressBag = new System.Collections.Concurrent.ConcurrentBag<long>();
            progressHandler = new Progress<long>(progress => progressBag.Add(progress));
            progressHandler.ProgressChanged += ProgressHandler_ProgressChanged;

        }
        public async Task UploadBlob(string connectionString, string container, FileInfo fileInfo)
        {

            using (var fs = fileInfo.Open(FileMode.Open, FileAccess.Read))
            {
                var clientOptions = new BlobClientOptions();
                clientOptions.Retry.NetworkTimeout = new TimeSpan(0, 0, 600);
                var blockBlobClient = new BlockBlobClient(connectionString, container, fileInfo.Name, clientOptions);

                var uploadOptions = new BlobUploadOptions();
                uploadOptions.ProgressHandler = progressHandler;
                await blockBlobClient.UploadAsync(fs, uploadOptions);
            }
        }

        private static void ProgressHandler_ProgressChanged(object sender, long e)
        {
            Debug.WriteLine($"Progress:{(e / 1000).ToString()} MB");
        }

    }
}

Upvotes: 3

Related Questions