Honchar Denys
Honchar Denys

Reputation: 1508

Windows Azure Storage Blobs to zip file with Express

I am trying to use this pluggin (express-zip). At the Azure Storage size we have getBlobToStream which give us the file into a specific Stream. What i do now is getting image from blob and saving it inside the server, and then res.zip it. Is somehow possible to create writeStream which will write inside readStream?

Upvotes: 1

Views: 909

Answers (2)

Michael Roberson - MSFT
Michael Roberson - MSFT

Reputation: 1621

Edit: The question has been edited to ask about doing this in express from Node.js. I'm leaving the original answer below in case anyone was interested in a C# solution.

For Node, You could use a strategy similar to what express-zip uses, but instead of passing a file read stream in this line, pass in a blob read stream obtained using createReadStream.

Solution using C#:

If you don't mind caching everything locally while you build the zip, the way you are doing it is fine. You can use a tool such as AzCopy to rapidly download an entire container from storage.

To avoid caching locally, you could use the ZipArchive class, such as the following C# code:

    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);
                    }
                }
            }
        }
    }

This creates a zip archive in Azure storage that contains multiple blobs without writing anything to disk locally.

Upvotes: 2

thrackle
thrackle

Reputation: 16

I'm the author of express-zip. What you are trying to do should be possible. If you look under the covers, you'll see I am in fact adding streams into the zip:

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

So something like this should work for you (prior to me adding support for this in the interface of the package itself):

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);
  });
});

Apologize if I've made horrible errors above; I haven't been on this for a bit.

Upvotes: 0

Related Questions