当前位置 : 主页 > 网页制作 > Nodejs >

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

来源:互联网 收集:自由互联 发布时间:2021-06-16
我想在S3存储桶上传GZip压缩的 JSON文件.我正在努力解决这个问题,有人可以帮我解决这个问题.因为,我正在尝试使用zlib npm模块进行Gzip压缩JSON文件但是没有得到一个方法来实现这个. 下面
我想在S3存储桶上传GZip压缩的 JSON文件.我正在努力解决这个问题,有人可以帮我解决这个问题.因为,我正在尝试使用zlib npm模块进行Gzip压缩JSON文件但是没有得到一个方法来实现这个.

下面是我在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);
            }
        });
    };

谢谢

另外,我使用下面的代码片段进行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);
        }
      })
网友评论