c# - 如何将附加 blob 上传到 azure 而不会出现大小限制异常?

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

public async Task<string> UploadDataAsync<T>(CloudBlobContainer container, string relativeUrl, List<T> reportData, string mimeType, string data, ILogger log)
    {
        string absoluteUri = string.Empty;
        byte[] bytes = Encoding.ASCII.GetBytes(data);
        using (var ms = new MemoryStream())
        {
            using (StreamWriter sw = new StreamWriter(ms))
            {
                sw.Write(data);
                sw.Flush();
                ms.Position = 0;

                CloudAppendBlob blob;
                blob = container.GetAppendBlobReference(relativeUrl);
                if (await blob.ExistsAsync())
                {
                    await blob.AppendBlockAsync(ms);
                    absoluteUri = blob.StorageUri.PrimaryUri.AbsoluteUri;
                }
                else
                {
                    CloudAppendBlob appBlob = await CreateEmptyFileAsync(container, relativeUrl, reportData, mimeType, log);
                    await appBlob.AppendBlockAsync(ms);
                    absoluteUri = appBlob.StorageUri.PrimaryUri.AbsoluteUri;
                }
            }
        }

        return absoluteUri;
    }

获取此异常 -

Microsoft.WindowsAzure.Storage.StorageException:“请求正文太大,超出了最大允许限制。” [外部代码] UploadService.cs 中的 PDCGeoTabFunction.Services.UploadService.UploadDataAsync(Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer、字符串、System.Collections.Generic.List、字符串、字符串、Microsoft.Extensions.Logging.ILogger) [外部代码] PDCGeoTabFunction.Services.SyncGeoTabDataService.GetZone(Geotab.Checkmate.API,字符串,字符串,字符串,Microsoft.Extensions.Logging.ILogger,字符串,字符串,字符串,字符串,System.Collections.Generic.List)

请帮忙。

最佳答案

您收到此错误的原因很可能是您尝试上传的数据超过 4MB。每个追加操作的请求负载大小最大可为 4MB。

来自REST API documentation :

Append Block uploads a block to the end of an existing append blob. The block of data is immediately available once the call succeeds on the server. A block may be up to 4 MiB in size.

请以这样的方式分割数据,确保每次调用 AppendBlockAsync 上传的数据不会超过 4MB。

关于c# - 如何将附加 blob 上传到 azure 而不会出现大小限制异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71507739/

相关文章:

C# 无法访问该文件,因为它已被其他进程使用

c# - "() => true"的结果是什么

azure - 我可以限制*特定*事件函数的并发吗?

azure - 使用标准 LRS 存储类调整 AKS 中的 PV/PVC 大小后,Artifactory > 监控 > 存储部分仍然显示旧的存储空间。怎么修?

azure - Blob 在 Azure 存储帐户中保留多长时间?

node.js - ExpressJS - 在开发中使用本地文件,在生产中使用 Azure Blob 存储中的文件

javascript - 如何在 JavaScript 中复制 GZipStream.Write()?

c# - SQLiteDataReader 类型亲和性错误?

用于集成测试的 Azure AD 承载 token ?

azure - 如何使用Azure数据工厂管道创建容器?