azurecloudazure-blob-storageuploading

Resume Uploading with Azure After Power Failure


how can i resume my uploading in Azure from last disconnect. in network failure i can continue but what i have to do after power failure when my system is restarted . how can i save current state of my software (which is uploading file to Azure). so if i save my state i can resume it from last point.I am using this code for uploading.The code is from internet.

private void UploadBigFile(){
    int count = 0, bufferSize = 40 * 1024, blockCount = 0;
    string filePath = @"D:\Dua.zip";
    List<string> blockIds = new List<string>();
    CloudStorageAccount storageAccount =     CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("mytestcontainer");
    container.CreateIfNotExists();
    byte[] bufferBytes = new byte[bufferSize];
    string fileName = Path.GetFileName(filePath);
    CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

    using (FileStream fileStream = File.OpenRead(filePath)){
        blockCount = (int)(fileStream.Length / bufferSize) + 1;
        Int64 currentBlockSize = 0;
        int currentCount = blockIds.Count();
        fileStream.Seek(bufferSize * currentCount, SeekOrigin.Begin);
        for (int i = blockIds.Count; i < blockCount; i++){
            currentBlockSize = bufferSize;
            if (i == blockCount - 1){
                currentBlockSize = fileStream.Length - bufferSize * i;
                bufferBytes = new byte[currentBlockSize];
            }
            if (currentBlockSize == 0) break;
            fileStream.Read(bufferBytes, 0, Convert.ToInt32(currentBlockSize));

            using (MemoryStream memoryStream = new MemoryStream(bufferBytes)){
                try{
                    string blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
                    blob.PutBlock(blockId, memoryStream, null);
                    blockIds.Add(blockId);
                    count++;
                    label1.Text = Convert.ToString(count);
                    label1.Refresh();
                }
                catch (Exception){}
            }
        }
    }
    blob.PutBlockList(blockIds);
}

Solution

  • If you're keeping track of the blocks, you can save the position and then restart it. Here's a blog article about uploading large files; the end of it tells exactly how to do what you're trying to do.