如何从 NodeJS 中的 IBM Object Store Bucket 读取 .zip 或 .gz 文件?
How to read a .zip or .gz file from IBM Object Store Bucket in NodeJS?
我在以下链接中找到的参考资料是关于从 Bucket 作为对象读取的。
如果我能像读取本地机器文件那样读取 createReadStream
,我就可以解压缩它。但是我找不到将对象存储桶内容(压缩文件)读取为 readStream
.
的方法
如果使用getObject操作读取,解压对象(或Buffer部分)后无法获取原文内容
有人可以帮忙吗?
使用代码:
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'))
}
});
响应正文:
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]}
链接:
如果您需要解压缩从 Object Store 获得的二进制内容,您可以尝试使用 jszip
包,如下所示:
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"
});
我在以下链接中找到的参考资料是关于从 Bucket 作为对象读取的。
如果我能像读取本地机器文件那样读取 createReadStream
,我就可以解压缩它。但是我找不到将对象存储桶内容(压缩文件)读取为 readStream
.
如果使用getObject操作读取,解压对象(或Buffer部分)后无法获取原文内容 有人可以帮忙吗?
使用代码:
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'))
}
});
响应正文:
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]}
链接:
如果您需要解压缩从 Object Store 获得的二进制内容,您可以尝试使用 jszip
包,如下所示:
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"
});