Azure Blob存储:DownloadToStreamAsync下载0kb流

时间:2014-07-19 17:47:08

标签: c# azure

查看下面的代码,我很惊讶我需要编写的样板代码,以确保库正确下载文件。

我有没有理由看到0kb的下载流,或者编写这样的方法是否正常?

 public static async Task<string> DownloadSASUriInputDataAsync(string workingDirectory, string sasUri)
    {
        Trace.TraceInformation("{0}", sasUri);
        var input = new CloudBlockBlob(new Uri(sasUri));
        input.ServiceClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromMilliseconds(100), 10);

        var fileName = Path.GetFileName(input.Name);

        await Retry.LinearAsync(async () =>
        {
            try
            {
                using (var ms = new MemoryStream())
                {
                    await input.DownloadToStreamAsync(ms);
                    ms.Seek(0, SeekOrigin.Begin);

                    if (ms.Length == 0)
                    {
                        throw new RunAlgorithmException("Downloaded file was 0 byte");
                    }

                    using (var fs = new FileStream(Path.Combine(workingDirectory, fileName), FileMode.Create, FileAccess.Write))
                    {
                        await ms.CopyToAsync(fs);
                    }
                }

                Trace.TraceInformation("downloaded file");

            }
            catch (StorageException ex)
            {

                Trace.TraceError("Failed to DownloadSASUriInputDataAsync : {0}", ex.ToString());
                throw;
            }
        }, TimeSpan.FromMilliseconds(500),10);

        return fileName;
    }

1 个答案:

答案 0 :(得分:0)

所有0kb流的问题是blob仍然被复制。

即使正在复制它们,仍然可以访问Blob,它将提供上述行为。

在尝试下载blob.CopyState完成或丢失之前添加检查可确保它作为SLA状态运行。