public static async Task <CloudBlockBlob> DownloadBlob(this CloudBlobContainer self, string path, string destinationFileName)
        {
            CloudBlockBlob blob = null;

            try
            {
                blob = self.GetBlockBlobReference(path);
                await blob.DownloadToFileAsync(destinationFileName, FileMode.Create);
            }
            catch (StorageException stex)
            {
                if (stex.RequestInformation != null &&
                    stex.RequestInformation.ExtendedErrorInformation != null &&
                    (stex.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.ContainerNotFound ||
                     stex.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.BlobNotFound))
                {
                    // Ignore the error
                }
                else
                {
                    throw;
                }
            }
            return(blob);
        }
Exemplo n.º 2
0
        public async Task CloudBlockBlobUploadDownloadFileWithFailures()
        {
            CloudBlockBlob blob     = this.testContainer.GetBlockBlobReference("blob1");
            CloudBlockBlob nullBlob = this.testContainer.GetBlockBlobReference("null");

            await this.DoUploadDownloadFileAsync(blob, 0);

            await this.DoUploadDownloadFileAsync(blob, 4096);

            await this.DoUploadDownloadFileAsync(blob, 4097);

            await TestHelper.ExpectedExceptionAsync <IOException>(
                async() => await blob.UploadFromFileAsync("non_existentCloudBlockBlobUploadDownloadFileWithFailures.file"),
                "UploadFromFileAsync requires an existing file");

            await TestHelper.ExpectedExceptionAsync <StorageException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudBlockBlobUploadDownloadFileWithFailures.file", FileMode.Create),
                "DownloadToFileAsync should not leave an empty file behind after failing.");

            Assert.IsFalse(System.IO.File.Exists("garbageCloudBlockBlobUploadDownloadFileWithFailures.file"));

            await TestHelper.ExpectedExceptionAsync <StorageException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudBlockBlobUploadDownloadFileWithFailures.file", FileMode.CreateNew),
                "DownloadToFileAsync should not leave an empty file behind after failing.");

            Assert.IsFalse(System.IO.File.Exists("garbageCloudBlockBlobUploadDownloadFileWithFailures.file"));

            byte[] buffer = GetRandomBuffer(100);
            using (FileStream file = new FileStream("garbageCloudBlockBlobUploadDownloadFileWithFailures.file", FileMode.Create, FileAccess.Write))
            {
                await file.WriteAsync(buffer, 0, buffer.Length);
            }
            await TestHelper.ExpectedExceptionAsync <IOException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudBlockBlobUploadDownloadFileWithFailures.file", FileMode.CreateNew),
                "DownloadToFileAsync should leave an empty file behind after failing, depending on the mode.");

            Assert.IsTrue(System.IO.File.Exists("garbageCloudBlockBlobUploadDownloadFileWithFailures.file"));
            System.IO.File.Delete("garbageCloudBlockBlobUploadDownloadFileWithFailures.file");

            await TestHelper.ExpectedExceptionAsync <StorageException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudBlockBlobUploadDownloadFileWithFailures.file", FileMode.Append),
                "DownloadToFileAsync should leave an empty file behind after failing depending on file mode.");

            Assert.IsTrue(System.IO.File.Exists("garbageCloudBlockBlobUploadDownloadFileWithFailures.file"));
            System.IO.File.Delete("garbageCloudBlockBlobUploadDownloadFileWithFailures.file");
        }