创建Gzip压缩的JSON文件并使用NodeJS在S3存储桶上上传

Ank*_*yal 5 gzip amazon-s3 node.js

我想将GZip压缩的JSON文件上传到S3存储桶中,对此我很挣扎,有人可以帮我吗。这个。

以下是我在S3上上传Gzip压缩JSON文件的上传方法:

    var uploadEntitlementDataOnS3 = function(next, event,
    jsonFileContent, filePath, results) {
    console.log("uploadEntitlementDataOnS3 function 
    started",jsonFileContent);
        var bufferObject = new 
    Buffer.from(JSON.stringify(jsonFileContent));
        var s3 = new AWS.S3();
        var params = {
            Bucket: configurationHolder.config.bucketName,
            Key: filePath,
            Body: bufferObject,
            CacheControl: 'no-cache',
            ContentType: "application/json",
            ContentEncoding: 'gzip'
        }
        s3.putObject(params, function(err, data) {
            if (err) {
                console.log(err, err.stack);
                next(err);
            } else {
                next(null, filePath);
            }
        });
    };
Run Code Online (Sandbox Code Playgroud)

谢谢

Ank*_*yal 0

另外,我使用下面的代码片段进行 gzip 压缩,如果我使用了错误的方法,请告诉我:

    var bufferObject = new Buffer.from(JSON.stringify(jsonFileContent));
    var zlib = require('zlib');
    zlib.gunzip(bufferObject, function(err, zipped) {
        if(err) {
          console.log("error",err);
          next(err);
        }
        else {
          console.log("zipped",zipped);
        }
      })
Run Code Online (Sandbox Code Playgroud)

  • 我怀疑您的问题是由于调用错误的函数引起的:您的代码在 JSON 字符串的缓冲区上调用“zlib.gunzip”,这就是您收到有关无效标头的错误的原因。正确使用的函数是“zlib.gzip”。 (5认同)