使用BlobContainerClient通过Blob存储将大量文件上传到Azure

时间:2020-09-09 19:44:03

标签: c# azure .net-core azure-storage-blobs topshelf

我想将大文件上传到Azure Blob存储(500-2000MB),我尝试使用以下代码来做到这一点:

private BlobContainerClient containerClient;    
public async Task<UploadResultDto> Upload(FileInfo fileInfo, string remotePath)
        {
            try
            {
                var blobClient = containerClient.GetBlobClient(remotePath + "/" + fileInfo.Name);

                var transferOptions = new StorageTransferOptions
                {
                    MaximumConcurrency = 1,
                    MaximumTransferSize = 10485760,
                    InitialTransferSize = 10485760
                };

                await using var uploadFileStream = File.OpenRead(fileInfo.FullName);
                
                await blobClient.UploadAsync(uploadFileStream, transferOptions: transferOptions);
                uploadFileStream.Close();

                return new UploadResultDto()
                {
                    UploadSuccessfull = true
                };

            }
            catch (Exception ex)
            {
                Log.Error(ex,$"Error while uploading File {fileInfo.FullName}");
            }

            return new UploadResultDto()
            {
                UploadSuccessfull = false
            };
        }

我立即收到以下消息:

The specified blob or block content is invalid.
RequestId:c5c2d925-701e-0035-7ce0-8691a6000000
Time:2020-09-09T19:33:40.9559646Z
Status: 400 (The specified blob or block content is invalid.)

如果我从InitialTransferSize中删除了StorageTransferOptions,则一段时间后会出现以下错误:

retry failed after 6 tries. (The operation was canceled.)

据我了解,新的SDK会以块的形式进行上传,因此应该由SDK来完成blockIds等的整个处理。还是我错了?

有人知道为什么这行不通吗?对于BlobContainerClient,我没有发现什么不同,仅对旧的cloudblobcontainer而言。

更新: 一些其他信息:

这是一个.netCore 3.1应用程序,它与作为Windows服务的Topshelf库一起运行

3 个答案:

答案 0 :(得分:0)

我创建了一个新的控制台应用程序,并使用您的代码进行了测试,效果很好。

1。确认您没有inconsistencies in assemblies。删除options: { plugins: { datalabels: { formatter: function(value, context) { return context.chart.data.labels[context.dataIndex]; } } } } 的早期版本并对其进行更新to the latest version

为什么您的Azure.Storage.Blobs私有的?您可以使用以下代码在containerClient方法中进行设置:

Upload

答案 1 :(得分:0)

我无法在12.6.0版上使用它...

我降级到Microsoft.Azure.Storage.Blob v11,并基于此线程实现了上传 https://stackoverflow.com/a/58741171/765766

现在对我来说很好

答案 2 :(得分:0)

InitialTransferSize 中删除 StorageTransferOptions 后问题的第二部分与问题 in this question 类似。

您可以通过如下设置 blob 客户端的超时来解决该问题:

var blobClientOptions = new BlobClientOptions
{
    Transport = new HttpClientTransport(new HttpClient { Timeout = Timeout.InfiniteTimeSpan }),
    Retry = { NetworkTimeout = Timeout.InfiniteTimeSpan }
};

InfiniteTimeSpan 可能有点矫枉过正,但至少它会证明这是否是问题所在。

这些设置为我消除了“重试 6 次后失败”错误,并在我开始使用 Azure.Storage.Blobs v12.8.0 软件包时上传

相关问题