首页 > 解决方案 > 执行 BlobClient.UploadAsync 时出现 HttpRequestException

问题描述

我有以下代码将文件上传到我的 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 文档 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

标签: c#azureazure-storageazure-blob-storageazure-sdk

解决方案


我也坚持这个问题。它的解决方案非常简单,只需将您的流位置设置为 0。

流.位置 = 0;

一切就绪,它将起作用。


推荐阅读