Search code examples
node.jsibm-cloudunzipgunzip

How to read a .zip or .gz file from IBM Object Store Bucket in NodeJS?


The references that I found in the following links say about reading from Bucket as an object.

If I could read as createReadStream as we do for local machine files, I can unzip it. But I could not find a way to read Object store bucket content ( a zipped file) as readStream.

If I read with getObject operation, I can not get the original text content once I unzip the object (or Buffer part) Can anyone please help?

The used code:

var params = {
    Bucket: bucketName,
    Key: itemName, 
};

return await cosClient.getObject(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else {
        console.log(typeof data, ">>>><<<<<<", data);           // successful response
            var decompress = zlib.createUnzip(data)
            console.log("<<<<<<<<<<<<<<<<<<<<<<<<<<<<", typeof decompress, decompress.toString())
            var json = JSON.stringify(decompress['_outBuffer'])
            console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>", typeof json, json)
            console.log("||||||||||||||||||||||||||||", json.toString('utf8'))
    }
});

The response body:

object >>>><<<<<<
{
  AcceptRanges: 'bytes',
  LastModified: 'Tue, 08 Dec 2020 01:53:06 GMT',
  ContentLength: '538',
  ETag: '"e55d7f8febfba8c58aec3d64cae70b0f"',
  ContentType: 'application/x-zip-compressed',
  Metadata: {},
  Body: <Buffer 50 4b 03 04 14 00 00 00 08 00 0c ba 76 51 d7 de aa 2a 7c 01 00 00 15 07 00 00 65 63 77 2e 6a 73 6f 6e ed 94 3d 4f c3 30 10 86 ... 480 more bytes> 
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<< object [object Object]
>>>>>>>>>>>>>>>>>>>>>>>>>>>> string {"type":"Buffer","data":[192,113,174,210,185,2,0,0,240,243,155,210, .... 0,0,0]}
|||||||||||||||||||||||||||| {"type":"Buffer","data":[192,113,174,210,185,2,0,0,240,243,155,210,185,2,0, .... 0,0,0]}

Links:

Calling the getObject operation

Get file contents of particular item


Solution

  • If you need to unzip a binary content you got from Object Store you can try to use jszip package like this:

    var JSZip = require("jszip");
    JSZip.loadAsync(data.Body)
    .then(function(zip) {
        // you now have every files contained in the loaded zip
        zip.file("hello.txt").async("string"); // a promise of "Hello World\n"
    });
    

    See example