Windows Azure 存储 Blob 使用 Express 压缩文件

Windows Azure Storage Blobs to zip file with Express

我正在尝试使用 this pluggin (express-zip). 在 Azure 存储大小下,我们有 getBlobToStream 将文件提供给特定的流。我现在所做的是从 blob 中获取图像并将其保存在服务器中,然后 res.zip 它。是否可以通过某种方式创建将在 readStream 中写入的 writeStream?

编辑: 问题已被编辑为从 Node.js 中询问有关在 express 中执行此操作的问题。如果有人对 C# 解决方案感兴趣,我将在下面留下原始答案。

对于 Node,您可以使用类似于 express-zip 使用的策略,但不是在 this line 中传递文件读取流,而是传递使用 createReadStream 获得的 blob 读取流。

使用C#的解决方案:

如果您不介意在构建 zip 时在本地缓存所有内容,那么您这样做的方式很好。您可以使用 AzCopy 等工具从存储中快速下载整个容器。

为了避免本地缓存,你可以使用ZipArchive class,例如下面的C#代码:

    internal static void ArchiveBlobs(CloudBlockBlob destinationBlob, IEnumerable<CloudBlob> sourceBlobs)
    {
        using (Stream blobWriteStream = destinationBlob.OpenWrite())
        {
            using (ZipArchive archive = new ZipArchive(blobWriteStream, ZipArchiveMode.Create))
            {
                foreach (CloudBlob sourceBlob in sourceBlobs)
                {
                    ZipArchiveEntry archiveEntry = archive.CreateEntry(sourceBlob.Name);

                    using (Stream archiveWriteStream = archiveEntry.Open())
                    {
                        sourceBlob.DownloadToStream(archiveWriteStream);
                    }
                }
            }
        }
    }

这会在 Azure 存储中创建一个包含多个 blob 的 zip 存档,而不会将任何内容写入本地磁盘。

我是 express-zip 的作者。你正在尝试做的事情应该是可能的。如果你深入了解,你会发现我实际上是在将流添加到 zip 中:

https://github.com/thrackle/express-zip/blob/master/lib/express-zip.js#L55

所以像这样的东西应该适合你(在我在包本身的界面中添加对此的支持之前):

var zip = zipstream(exports.options);
zip.pipe(express.response || http.ServerResponse.prototype); // res is a writable stream

var addFile = function(file, cb) {
  zip.entry(getBlobToStream(), { name: file.name }, cb);
};

async.forEachSeries(files, addFile, function(err) {
  if (err) return cb(err);
  zip.finalize(function(bytesZipped) {
    cb(null, bytesZipped);
  });
});

如果我在上面犯了可怕的错误,我深表歉意;我好久没关注这个了。