【问题标题】:HttpRequestException while executing BlobClient.UploadAsync执行 BlobClient.UploadAsync 时出现 HttpRequestException
【发布时间】:2020-05-13 22:19:10
【问题描述】:

我有以下代码可以将文件上传到我的 Azure 存储:

读取文件流如下:

   FileStream fileStream = File.OpenRead(filePath);

并传递给函数 =>

 public async Task<Response<BlobContentInfo>> UploadBlob(string containerName, string blobName, Stream stream, string contentType,
            IDictionary<string, string> metadata = null)
        {
            IDictionary<string, string> metadataEncoded = new Dictionary<string, string>();
            if (metadata != null)
            {
                foreach (KeyValuePair<string, string> keyValuePair in metadata)
                {
                    string encodedValue = TextUtilities.Base64Encode(keyValuePair.Value);
                    metadataEncoded.Add(keyValuePair.Key, encodedValue);
                }
            }
            await using (stream)
            {
                BlobClient blobClient = GetBlobClient(containerName, blobName);
                BlobHttpHeaders httpHeaders = new BlobHttpHeaders { ContentType = contentType };
                BlobRequestConditions conditions = new BlobRequestConditions();
                Response<BlobContentInfo> uploadAsync = await blobClient.UploadAsync(stream, httpHeaders, metadataEncoded, conditions);
                stream.Close();
                return uploadAsync;
            }
        }

如果我尝试上传像这个示例 pdf => http://www.africau.edu/images/default/sample.pdf 这样的任意文件,它可以正常工作。但是由于某种原因,如果我尝试像在此页面中那样上传 pdf 文件 =>
https://docs.microsoft.com/en-us/dotnet/core/
Dot.NET Core Documentation PDF

我收到以下异常:

Inner Exception 1:
HttpRequestException: Error while copying content to a stream.

Inner Exception 2:
IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..

Inner Exception 3:
SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

我不认为这与文件大小有关,因为我也尝试了一些其他大尺寸的 .pdf 文件,它们被推送到 Azure 存储。

我在这里遗漏了一些具体的东西吗?我还从https://docs.microsoft.com/en-us/azure/sql-database/ 下载了.pdf,并从await blobClient.UploadAsync() 行收到了同样的错误

注意:作为参考,我发现了这个开放的 GitHub 问题,但尚未解决。 https://github.com/Azure/azure-sdk-for-net/issues/9212

【问题讨论】:

  • 是否可以包含无法上传的 PDF 链接?您的第二个链接仅指向 .Net Core 文档页面。
  • 页面左下角是下载链接。

标签: c# azure azure-storage azure-blob-storage azure-sdk


【解决方案1】:

我也遇到了这个问题。它的解决方案很简单,只需将你的流位置设置为0即可。

stream.Position = 0;

一切就绪,它会工作的。

【讨论】:

  • 嗨,Deepak,欢迎来到 SO!在回答下一个问题之前,请阅读writing a good answer!享受您的住宿:)
猜你喜欢
  • 2014-10-02
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 2017-08-12
  • 2014-03-13
  • 2020-05-08
相关资源
最近更新 更多