【发布时间】:2022-01-18 22:23:55
【问题描述】:
我在 API 后面运行了一些代码,该 API 循环遍历 Azure Blob 存储上的文件列表,将它们压缩并将最终的 Zip 保存到同一个存储帐户。然后我提供一个指向 Zip 文件的链接供我的用户访问。
只要文件很小,此解决方案就可以正常工作。但是,有很多文件在 2-5 GB 范围内,一旦测试这些文件,我就会收到内存不足异常错误:
'数组尺寸超出支持范围。'
我已经看到 OneDrive 和 GoogleDrive 等系统非常快速地创建了这些文件,我渴望为我的用户创造这种体验。但是我也可以在存档准备好下载时通知用户,即使是几分钟后,因为我会收到他们的电子邮件。
以下是简化并在控制台应用程序中运行的代码版本:
using Microsoft.WindowsAzure.Storage;
using System.IO.Compression;
var account = CloudStorageAccount.Parse("ConnectionString");
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("ContainerName");
var blob = container.GetBlockBlobReference("ZipArchive.zip");
using (var stream = await blob.OpenWriteAsync())
using (var zip = new ZipArchive(stream, ZipArchiveMode.Create))
{
var files = new string[] {
"files/psds/VeryLargePsd_1.psd",
"files/psds/VeryLargePsd_2.psd",
"files/psds/VeryLargePsd_3.psd",
"files/zips/VeryLargeZip_1.zip",
"files/zips/VeryLargeZip_2.zip"
};
foreach (var file in files)
{
var sourceBlob = container.GetBlockBlobReference(file);
var index = file.LastIndexOf('/') + 1;
var fileName = file.Substring(index, file.Length - index);
var entry = zip.CreateEntry(fileName, CompressionLevel.Optimal);
await sourceBlob.FetchAttributesAsync();
byte[] imageBytes = new byte[sourceBlob.Properties.Length];
await sourceBlob.DownloadToByteArrayAsync(imageBytes, 0);
using (var zipStream = entry.Open())
zipStream.Write(imageBytes, 0, imageBytes.Length);
}
}
【问题讨论】:
标签: c# asp.net .net azure azure-blob-storage