C# Azure.Storage.Blobs SDK 如何列出和压缩容器中的所有文件并将压缩文件存储在另一个容器中
Posted
技术标签:
【中文标题】C# Azure.Storage.Blobs SDK 如何列出和压缩容器中的所有文件并将压缩文件存储在另一个容器中【英文标题】:C# Azure.Storage.Blobs SDK How to list and zip all files in a container and store the zip in another container 【发布时间】:2021-10-11 02:06:20 【问题描述】:我在一个容器中有大约 100 个文件。我想调用一个函数来压缩所有文件并将其发送到另一个容器。我正在使用 Azure.Storage.Blobs 版本 12.9.1
var blob = container.GetBlockBlobReference(outputFilename);
using (var stream = await blob.OpenWriteAsync())
using (var zip = new ZipArchive(stream, ZipArchiveMode.Create))
for (int i = 0; i < 2000; i++)
using (var randomStream = CreateRandomStream(2))
var entry = zip.CreateEntry($"i.zip", CompressionLevel.Optimal);
using (var innerFile = entry.Open())
await randomStream.CopyToAsync(innerFile);
这是针对旧版本的。我们如何使用新的 sdk 来实现它
【问题讨论】:
【参考方案1】:你能试试这个代码吗?
解决方案 1: 尝试使用此代码生成 Zip 文件。生成zip文件后上传到容器
public void ZipFilesToResponse(HttpResponseBase response, IEnumerable<Asset> files, string zipFileName)
using (var zipOutputStream = new ZipOutputStream(response.OutputStream))
zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
response.BufferOutput = false;
response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
response.ContentType = "application/octet-stream";
foreach (var file in files)
var entry = new ZipEntry(file.FilenameSlug())
DateTime = DateTime.Now,
Size = file.Filesize
;
zipOutputStream.PutNextEntry(entry);
storageService.ReadToStream(file, zipOutputStream);
response.Flush();
if (!response.IsClientConnected)
break;
zipOutputStream.Finish();
zipOutputStream.Close();
response.End();
更多详情请参考link
解决方案 2:也尝试使用此代码
using System;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace SO68566758
class Program
private const string connectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
private const string sourceContainer = "source-container";
private const string targetContainer = "target-container";
private const string outputBlobName = "backup.zip";
static async Task Main(string[] args)
BlobServiceClient serviceClient = new BlobServiceClient(connectionString);
BlobContainerClient sourceContainerClient = serviceClient.GetBlobContainerClient(sourceContainer);
BlobContainerClient targetContainerClient = serviceClient.GetBlobContainerClient(targetContainer);
var blobs = sourceContainerClient.GetBlobsAsync();
using (var fs = new FileStream(outputBlobName, FileMode.OpenOrCreate))
using (var zipOutputStream = new ZipOutputStream(fs))
await foreach (var blob in blobs)
var blobName = blob.Name;
var blobClient = sourceContainerClient.GetBlobClient(blobName);
//var downloadResponse = await blobClient.DownloadAsync();
//var streamContent = downloadResponse.Value.Content;
var entry = new ZipEntry(blobName);
zipOutputStream.PutNextEntry(entry);
await blobClient.DownloadToAsync(zipOutputStream);
BlobClient targetBlob = targetContainerClient.GetBlobClient(outputBlobName);
using (FileStream fs = new FileStream(outputBlobName, FileMode.Open))
await targetBlob.UploadAsync(fs);
【讨论】:
以上是关于C# Azure.Storage.Blobs SDK 如何列出和压缩容器中的所有文件并将压缩文件存储在另一个容器中的主要内容,如果未能解决你的问题,请参考以下文章
C# Azure.Storage.Blobs SDK 如何列出和压缩容器中的所有文件并将压缩文件存储在另一个容器中
无法使用 Azure.Storage.Blobs NuGet 包将文件上传到 Azure Blob 存储
如何从 Azure.Storage.Blobs 模拟 GetBlobsByHierarchy()?