【发布时间】:2017-02-20 18:17:07
【问题描述】:
我正在尝试使用连接到进度条的 .DownloadToStreamAsync() 方法从 Azure Blob 存储下载文件的完整示例。
我找到了对 azure storage sdk 旧实现的引用,但它们不能与较新的 sdk(已实现这些异步方法)编译或不适用于当前的 nuget 包。
我是 .NET 中异步/等待线程的新手,想知道是否有人可以帮助我完成以下操作(在 Windows 窗体应用程序中)并展示我如何“挂钩”到文件下载...我看到一些示例不使用 .DownloadToStream 方法,而是下载 blob 文件的块。但我想知道,因为这些新的 ...Async() 方法存在于较新的 Storage SDK 中,如果有的话可以做一些更聪明的事情吗?
所以假设下面的工作(非异步),我还需要做什么才能使用 blockBlob.DownloadToStreamAsync(fileStream);方法,这甚至是正确的使用方法,我怎样才能得到进展?
理想情况下,我可以通过任何方式来挂钩 blob 下载的进度,这样我就可以在大下载时更新 Windows 窗体 UI。所以如果以下方法不正确,请赐教:)
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Retrieve reference to a blob named "photo1.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("photo1.jpg");
// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
{
blockBlob.DownloadToStream(fileStream);
}
使用Gaurav 建议的很棒的建议method(下载1mb 块),我已经实现了使用后台工作人员进行下载,这样我就可以随时更新UI。
do 循环中的主要部分将范围下载到流中,然后将流写入到我在原始示例中没有触及的文件系统,但我添加了代码来更新工作进程并监听工作进程取消(中止下载).. 不确定这是否是问题所在?
为了完整起见,下面是 worker_DoWork 方法中的所有内容:
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
object[] parameters = e.Argument as object[];
string localFile = (string)parameters[0];
string blobName = (string)parameters[1];
string blobContainerName = (string)parameters[2];
CloudBlobClient client = (CloudBlobClient)parameters[3];
try
{
int segmentSize = 1 * 1024 * 1024; //1 MB chunk
var blobContainer = client.GetContainerReference(blobContainerName);
var blob = blobContainer.GetBlockBlobReference(blobName);
blob.FetchAttributes();
blobLengthRemaining = blob.Properties.Length;
blobLength = blob.Properties.Length;
long startPosition = 0;
do
{
long blockSize = Math.Min(segmentSize, blobLengthRemaining);
byte[] blobContents = new byte[blockSize];
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadRangeToStream(ms, startPosition, blockSize);
ms.Position = 0;
ms.Read(blobContents, 0, blobContents.Length);
using (FileStream fs = new FileStream(localFile, FileMode.OpenOrCreate))
{
fs.Position = startPosition;
fs.Write(blobContents, 0, blobContents.Length);
}
}
startPosition += blockSize;
blobLengthRemaining -= blockSize;
if (blobLength > 0)
{
decimal totalSize = Convert.ToDecimal(blobLength);
decimal downloaded = totalSize - Convert.ToDecimal(blobLengthRemaining);
decimal blobPercent = (downloaded / totalSize) * 100;
worker.ReportProgress(Convert.ToInt32(blobPercent));
}
if (worker.CancellationPending)
{
e.Cancel = true;
blobDownloadCancelled = true;
return;
}
}
while (blobLengthRemaining > 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是可行的,但在更大的文件(例如 30mb)上,我有时会收到“无法写入文件,因为在另一个进程中打开错误......”并且进程失败..
【问题讨论】:
标签: c# azure azure-blob-storage