node.js - 如何将字符串作为 gzip Nodejs 上传到 s3

我有一个从 Redis 读取字符串数组的 node.JS 程序。 我需要将此字符串以 GZIP 格式上传到我在 AWS s3 中的存储桶,而无需在上传前在本地创建 gzip 文件。

基本上,我希望将读取的字符串从 Redis 流式传输到 gzip 压缩的 s3 存储桶。

除了上述问题,我想知道以这种方式编程的有效和推荐方式是什么,因此将存储在 S3 中的文件最大为 64 MB,并且有额外的数据,将创建附加文件(也限制为最大 64MB)。 在下面的示例中,我展示了如何从 Redis 键中读取要存储在 s3 gzip 中的值限制为 64 MB:

client.lrange(key, 0, -1, (error, arrayStringValue) => {
            if (arrayStringValue == null || !arrayStringValue.length) {
                client.del(key);
                return console.log("removing key, no values");
            }
            if (error) reject(error);
            console.log("finish iterating");
            impressionsRecorded = true;
            //compress 
            uploadToS3(arrayStringValue, "bucket/key", null);

基本上我缺少的是 uploadToS3 方法的正确实现

最佳答案

我不得不解决同样的问题。这是我在 Node.js 中的解决方案。

const zlib = require('zlib');

const uncompressedString = 'Hi there';

const compressedStringAsBuffer = zlib.gzipSync(uncompressedString);

const AWS = require('aws-sdk');
const s3 = new AWS.S3();

const s3Params = {
      Bucket: 'my-bucket',
      Body: compressedStringAsBuffer,
      Key: 'my-compressed-string.txt.gzip', // .gzip isn't required
      ContentType: 'text/plain', // change the ContentType if you want to serve HTML, CSS, JS, etc.
      ContentEncoding: 'gzip' // that's important
    };

await s3.putObject(s3Params).promise();

https://stackoverflow.com/questions/54270169/

相关文章:

amazon-web-services - "kubectl"未从我的本地 Windows 工作站连

python - FlaskForm 验证码 : checking if a user alread

angular - 找不到自定义管道 ionic 4 |找不到管道

reactjs - 在导航更改时滚动恢复到顶部

c# - 如何从 dapper 返回的复杂对象中修剪所有字符串

angular - ng2图表多图表更新

python - 无法使用 python 3.72 连接 firebase

postgresql - . 时如何将密码传递给 pg_dump 10主目录中的 pgpass 不是

python - 使用 Python 插入多个 MySQL 记录。错误 : "Python ' tu

python - 如何组合两个 Python Try 异常