上传大文件 Azure Blob .net SDK v12 问题

Posted

技术标签:

【中文标题】上传大文件 Azure Blob .net SDK v12 问题【英文标题】:Upload Larget File Azure Blob .net SDK v12 Problem 【发布时间】:2021-03-31 22:19:44 【问题描述】:

我正在尝试使用 .net SDK V12 将大文件存储到 Azure Blob。这个程序可以上传小文件,没有异常,但是大文件抛出异常。你有解决这个问题的办法吗?

BlobClient blobClient = containerClient.GetBlobClient(uploadFileName);

using (FileStream uploadFileStream = File.OpenRead(localFilePath))

    await blobClient.UploadAsync(uploadFileStream, true);
    var meta = new Dictionary<string, string>();
    meta["LastWriteTime"] = new FileInfo(localFilePath).LastWriteTime.ToString("yyyy/MM/dd HH:mm:ss");
    meta["CreationTime"] = new FileInfo(localFilePath).CreationTime.ToString("yyyy/MM/dd HH:mm:ss");
    await blobClient.SetMetadataAsync(meta);

我想知道的不是.NET SDK V11的解决方案,而是V12。

这个异常如下,文件大小为91.6MB

System.AggregateException: Retry failed after 6 tries. (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.) (The operation was canceled.)
 ---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
 ---> System.Net.Http.HttpRequestException: Error while copying content to a stream.
 ---> System.IO.IOException: Unable to read data from the transport connection: 
 ---> System.Net.Sockets.SocketException (995): 
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Security.SslStream.<WriteSingleChunk>g__CompleteAsync|210_1[TWriteAdapter](ValueTask writeTask, Byte[] bufferToReturn)
   at System.Net.Security.SslStream.WriteAsyncChunked[TWriteAdapter](TWriteAdapter writeAdapter, ReadOnlyMemory`1 buffer)
   at System.Net.Security.SslStream.WriteAsyncInternal[TWriteAdapter](TWriteAdapter writeAdapter, ReadOnlyMemory`1 buffer)
   at System.Net.Http.HttpConnection.WriteAsync(ReadOnlyMemory`1 source)
   at System.IO.Stream.CopyToAsyncInternal(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at Azure.Core.RequestContent.StreamContent.WriteToAsync(Stream stream, CancellationToken cancellation)
   at Azure.Core.Pipeline.HttpClientTransport.PipelineRequest.PipelineContentAdapter.SerializeToStreamAsync(Stream stream, TransportContext context)
   at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask)

【问题讨论】:

先分享异常怎么样?! 你的文件有多大?我用using (FileStream file = File.OpenRead(path)) await blob.UploadAsync(file); 成功上传了文件(大约443.53 MiB)。 @silent 感谢您的评论。我添加了例外。 @PamelaPeng 谢谢你的评论。文件大小为 91.6MB。 【参考方案1】:

我在使用StorageConnectionString 时遇到了同样的错误,如下所示:

BlobServiceClient blobServiceClient = new BlobServiceClient(StorageConnectionString);
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(BlobContainerName);
BlobClient blob = container.GetBlobClient(BlobName);

// Open the file and upload its data
using (FileStream file = File.OpenRead(path))

    await blob.UploadAsync(file);

我改用StorageSharedKeyCredential,它成功了。

string accountName = "";
string accountKey = "";
Uri serviceUri = new Uri("https://xxx.blob.core.windows.net/");

// Create a SharedKeyCredential that we can use to authenticate
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);

// Create a client that can authenticate with a connection string
BlobServiceClient blobServiceClient = new BlobServiceClient(serviceUri, credential);

// BlobServiceClient blobServiceClient = new BlobServiceClient(StorageConnectionString);
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(BlobContainerName);
BlobClient blob = container.GetBlobClient(BlobName);

// Open the file and upload its data
using (FileStream file = File.OpenRead(path))

    await blob.UploadAsync(file);

【讨论】:

谢谢你的回答。我会试试这个。如果你知道,你知道为什么使用 StorageConnectionString 有效吗?我想知道原因。 抱歉,我不知道为什么。我用最新版本的storage SDK试了这两种方法,第一种报错,第二种成功。

以上是关于上传大文件 Azure Blob .net SDK v12 问题的主要内容,如果未能解决你的问题,请参考以下文章

与 v11 相比,使用 v12 将文件上传到 Azure Blob 存储 SDK 的 ASP.NET Core 使用更高的内存

如何将大文件上传到 Azure Blob 存储 (.NET Core)

如何使用 .NET v12 SDK 在 Azure Blob 存储中上传具有指定 ContentType 的 Blob?

Azure Blob PHP SDK - 直接从自定义多部分 API 请求上传到 Azure 存储

Azure Blob 存储 - 从上传控制 .NET 上传

使用 .NET SDK 获取 Azure Blob 存储区域?