Azure存储: BlobClient keeps resetting itself while uploading large files

标签 azure xamarin azure-storage azure-blob-storage

我正在尝试从连接到快速 Wifi 连接的手机上传 200 MB 的视频文件。我正在使用适用于 .NET 的 Azure 存储 SDK v12,但以下代码在上传进度约为 30% 后不断重置自身。当重置发生时,进度从0开始,不会抛出异常。

await blobClient.UploadAsync(stream, progressHandler: new Progress<long>(progress =>
{
     // show progress bar
}), cancellationToken: cancellationToken);

v12支持上传大文件吗?如果我没记错的话,旧的 API 能够上传单个 block 。我的印象是上述上传方法会隐式处理分块。

如何使用最新的sdk上传大文件?

附注我尝试传递具有非常高并发性和 1 MB 传输大小的 StorageTransferOptions,但没有任何区别。

编辑:等待很长时间后,我能够抛出异常。我看到多个任务被取消,因为

Cannot access a disposed object.
Object name: 'MobileAuthenticatedStream'.

  at Mono.Net.Security.MobileAuthenticatedStream.StartOperation (Mono.Net.Security.MobileAuthenticatedStream+OperationType type, Mono.Net.Security.AsyncProtocolRequest asyncRequest, System.Threading.CancellationToken cancellationToken) [0x00245] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:410 
  at System.Net.Http.HttpConnection.WriteAsync (System.ReadOnlyMemory`1[T] source) [0x00118] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs:1008 
  at System.IO.Stream.CopyToAsyncInternal (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) [0x000e7] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/shared/System/IO/Stream.cs:152 
  at Azure.Core.RequestContent+StreamContent.WriteToAsync (System.IO.Stream stream, System.Threading.CancellationToken cancellation) [0x00094] in <7b1dc95b0b4841539beb48023c1128d3>:0 
  at Azure.Core.Pipeline.HttpClientTransport+PipelineRequest+PipelineContentAdapter.SerializeToStreamAsync (System.IO.Stream stream, System.Net.TransportContext context) [0x0007c] in <7b1dc95b0b4841539beb48023c1128d3>:0 
  at System.Net.Http.HttpContent.CopyToAsyncCore (System.Threading.Tasks.ValueTask copyTask) [0x00022] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpContent.cs:361 

因此,blobClient 中似乎内置了并行化功能,但它仍然失败。此外,我无法使用 https://www.nuget.org/packages/Microsoft.Azure.Storage.DataMovement因为它不适用于 Xamarin Forms

最佳答案

如果您想将文件分块上传到Azure Blob存储,请引用以下代码

public async Task upload(Stream stream){
            string connectionString = "";
            string containerName = "upload";
            string blobName = "";
            BlockBlobClient blobClient = new BlockBlobClient(connectionString, containerName, blobName);
           
            List<string> blockList = new List<string>();

                while (true) {
                    byte[] b = new byte[1024 * 1024];
                    var n = await stream.ReadAsync(b, 0, 1024 * 1024);
                    if (n == 0) break;
                    string blockId = Guid.NewGuid().ToString();
                    string base64BlockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId));
                    await blobClient.StageBlockAsync(base64BlockId, new MemoryStream(b, true));
                    blockList.Add(base64BlockId);
                }

                await blobClient.CommitBlockListAsync(blockList);

   
}

更新

如果您想使用sas token,请引用以下代码

var uri = new Uri($"https://{storageAccountName}.blob.core.windows.net/{containerName}/{blobName}?{sasToken}");
BlockBlobClient blobClient = new BlockBlobClient(uri);

关于Azure存储: BlobClient keeps resetting itself while uploading large files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65499942/

相关文章:

azure - 无法在 Web 配置中使用 applicationInitialization 预热页面

android - 扩展 BaseAdapter 的 CustomAdapter 出错

Azure 文件服务与 Blob 存储 - 性能方面

azure - 将 Azure 存储帐户迁移到其他订阅

asp.net - 在 Windows Azure Blob 存储中托管 ASPNET 页面?

arrays - PowerShell 结果数组重复输出

azure - 使用 Terraform 的 Azure 数据工厂诊断设置的资源特定目标表

azure - 在 Azure 门户中的哪里可以为 azure 应用服务启用 DDoS IP 保护?例如 API

android - 无法在 Mac OS X El Capitan 上启动虚拟设备

android - (Android Xamarin) 获取资源字符串值而不是 int