我已将一些文件上传到 Azure 存储。当我使用 SAS 获取文件的 URL 并将该 URL 放入浏览器中时,我会看到该文件。当我尝试从 Azure Functions 应用程序访问内容时,内容大小返回为零字节。实际源文件大约3兆。我尝试了多种下载内容的方法,但我不断遇到响应代码为 200 的 0 字节响应流。知道为什么会发生这种情况吗?
async Task<MemoryStream> GetUrlAsMemoryStream(String sourceUrl)
{
MemoryStream ms = new MemoryStream();
try
{
var handler = new HttpClientHandler();
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli;
using (HttpClient httpClient = new HttpClient(handler))
{
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "br");
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
var response = await httpClient.GetAsync(sourceUrl);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Error getting image from URL: {response.ReasonPhrase}");
}
var stream = await response.Content.ReadAsStreamAsync();
await stream.CopyToAsync(ms);
ms.Position = 0;
if(ms.Length == 0)
{
_logger.LogWarning($"The stream is empty::{sourceUrl}");
}
return ms;
}
}
catch (Exception exc)
{
_logger.LogError("Error getting image from URL", exc.Message);
ms.Dispose();
throw;
}
}
我不知道为什么会出现这种行为。但是当它发生时,如果我在 200 毫秒后再次读取相同的 URL,那么它就会成功。当我收到 0 字节流时,我最终实现了重试。