我有以下代码上传文件到我的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.eduimagesdefaultsample.pdf 它工作正常。但由于某些原因,如果我尝试上传pdf文件,如在这个页面=> https:/docs.microsoft.comen-usdotnetcore。 Dot.NET核心文档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.comen-usazuresql-database。 并从该行收到同样的错误 await blobClient.UploadAsync()
注:作为参考,我找到了这个开放的GitHub问题,但还没有解决。https:/github.comAzureazure-sdk-for-netissues9212。
我也曾卡过这个问题。它的解决方法很简单,只要将你的流位置设置为0即可。
stream.Position = 0。
所有的设置,它将工作。