首页 > 解决方案 > 通过 Cloud Functions 从 Cloud Storage 解压缩文件

问题描述

我的 Google Cloud Storage中有 gzip 文件,我必须检查使用Cloud Functions压缩的文件的校验和。

我开始使用此示例中的解压缩示例,但它仅适用于 ZIP 文件而非 gzip:

gcsSrcObject.createReadStream()
    .pipe(unzipper.Parse())
    .pipe(stream.Transform({
        objectMode: true,
        transform: function (entry, e, callback) {
            var filePath = entry.path;
            var type = entry.type;
            var size = entry.size;
            console.log(`Found ${type}: ${filePath}`);
            console.log(`Unzipping to: ${TEMP}/${prefix}/${filePath}`)
            var gcsDstObject = dstBucket.file(`${TEMP}/${prefix}/${filePath}`);
            entry
                .pipe(gcsDstObject.createWriteStream())
                .on('error', function (err) {
                    console.log(`Error: ${err}`);
                })
                .on('finish', function () {
                    console.log('Complete');
                    callback();
                });
        }
    }));

我还阅读了有关本机存储功能(gsutil cp)的文档,但它只允许您从本地复制 GZIP 文件。

标签: node.jsgoogle-cloud-platformgoogle-cloud-storagegoogle-cloud-functions

解决方案


请记住,如果您的文件使用“Content-Encoding: gzip”存储在 GCS 中,那么您可以透明地即时解压缩它们。GCS 将此称为“转码”

https://cloud.google.com/storage/docs/transcoding


推荐阅读