|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | using System.IO.Compression; |
| 5 | +using System.Net; |
5 | 6 | using System.Text; |
| 7 | +using Azure; |
6 | 8 | using Azure.Core; |
7 | 9 | using Azure.Storage.Blobs; |
8 | 10 | using Azure.Storage.Blobs.Models; |
@@ -117,20 +119,30 @@ public override async Task<string> DownloadAsync(string token, CancellationToken |
117 | 119 |
|
118 | 120 | BlobClient blob = this.containerClient.GetBlobClient(name); |
119 | 121 |
|
120 | | - using BlobDownloadStreamingResult result = await blob.DownloadStreamingAsync(cancellationToken: cancellationToken); |
121 | | - Stream contentStream = result.Content; |
122 | | - bool isGzip = string.Equals( |
123 | | - result.Details.ContentEncoding, ContentEncodingGzip, StringComparison.OrdinalIgnoreCase); |
| 122 | + try |
| 123 | + { |
| 124 | + using BlobDownloadStreamingResult result = await blob.DownloadStreamingAsync(cancellationToken: cancellationToken); |
| 125 | + Stream contentStream = result.Content; |
| 126 | + bool isGzip = string.Equals( |
| 127 | + result.Details.ContentEncoding, ContentEncodingGzip, StringComparison.OrdinalIgnoreCase); |
| 128 | + |
| 129 | + if (isGzip) |
| 130 | + { |
| 131 | + using GZipStream decompressed = new(contentStream, CompressionMode.Decompress); |
| 132 | + using StreamReader reader = new(decompressed, Encoding.UTF8); |
| 133 | + return await ReadToEndAsync(reader, cancellationToken); |
| 134 | + } |
124 | 135 |
|
125 | | - if (isGzip) |
| 136 | + using StreamReader uncompressedReader = new(contentStream, Encoding.UTF8); |
| 137 | + return await ReadToEndAsync(uncompressedReader, cancellationToken); |
| 138 | + } |
| 139 | + catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound) |
126 | 140 | { |
127 | | - using GZipStream decompressed = new(contentStream, CompressionMode.Decompress); |
128 | | - using StreamReader reader = new(decompressed, Encoding.UTF8); |
129 | | - return await ReadToEndAsync(reader, cancellationToken); |
| 141 | + throw new InvalidOperationException( |
| 142 | + $"The blob '{name}' was not found in container '{container}'. " + |
| 143 | + "The payload may have been deleted or the container was never created.", |
| 144 | + ex); |
130 | 145 | } |
131 | | - |
132 | | - using StreamReader uncompressedReader = new(contentStream, Encoding.UTF8); |
133 | | - return await ReadToEndAsync(uncompressedReader, cancellationToken); |
134 | 146 | } |
135 | 147 |
|
136 | 148 | /// <inheritdoc/> |
|
0 commit comments