电源故障后继续使用Azure上传

时间:2015-09-30 07:51:47

标签: azure cloud azure-storage-blobs uploading

如何从上次断开连接恢复我在Azure中的上传。在网络故障中,我可以继续,但是当我的系统重新启动电源故障后我必须做什么。如何保存我的软件的当前状态(将文件上传到Azure)。所以,如果我保存我的状态,我可以从最后一点恢复它。我正在使用此代码上传。代码来自互联网。

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);
}

1 个答案:

答案 0 :(得分:1)

如果要跟踪块,可以保存位置然后重新启动。这是一篇关于uploading large files的博客文章;它的结尾告诉你如何做你想做的事情。