c# - 执行 BlobClient.UploadAsync 时出现 HttpRequestException

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

我有以下代码将文件上传到我的 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://learn.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://learn.microsoft.com/en-us/azure/sql-database/下载了.pdf并从行 await blobClient.UploadAsync()

收到相同的错误

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

最佳答案

我也被这个问题困扰了。解决方法很简单,只需将你的流位置设置为0即可。

stream.Position = 0;

一切就绪,就可以工作了。

关于c# - 执行 BlobClient.UploadAsync 时出现 HttpRequestException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692408/

相关文章:

azure 跟踪日志有间隙

Azure Blob 容器

C# 没有 SortedList<T> 吗?

c# - DP 更改值后启用并聚焦已禁用的控件

azure - logicapp 中需要存储帐户名称

将 OAuth2 代码交换为访问 token 时,Azure AD 返回非 JWT token

具有尾递归优化的 C# 编译?

c# - UWP ProgressBar 和绑定(bind)

python - Databricks异常: Total size of serialized results is bigger than spark. driver.maxResultsSize

visual-studio - 不使用模拟器安装 Azure SDK