如何将字符串作为 gzip Nodejs 上传到 s3
How to upload a string to s3 as gzip Nodejs
我有一个 node.JS 程序可以从 Redis 中读取字符串数组。
我需要将此字符串以 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();
我正在扩展 Jan 为 IBM Cloud Object Storage (COS) 提供的答案:
让我们处理 IBM COS 的凭据:
var config = {
endpoint: 's3.us-south.cloud-object-storage.appdomain.cloud',
apiKeyId: 'put your key here',
serviceInstanceId: 'put serviceInstanceID here',
};
//bucket settings
var evaluation_bucket_name = 'evaluation.data'
现在假设您有一个字符串,其中包含要压缩并提交给 IBM COS 的数据(您的数据可以是 JSON 格式或任何其他格式的用户数据)。这里我有一个名为 aggregated_records
的变量,格式为 JSON。
toBeSubmitted = JSON.stringify(aggregated_records)
const zlib = require('zlib');
const toBeSubmittedCompressed = zlib.gzipSync(toBeSubmitted);
var object_name = 'aggregated_records.json.gzip'
现在,我们将其提交给IBM COS。
CreateObject(evaluation_bucket_name, object_name, toBeSubmittedCompressed)
.then(function () {
console.log(`###The object has been successfully submitted to the IBM Object Storage!`)
})
.catch(function (err) {
console.error('An error occurred:', util.inspect(err))
});
现在,让我们来看看提交功能
function CreateObject(bucket_name, object_name, object_content) {
console.log('Creating object');
return cos.putObject({
Bucket: bucket_name,
Key: object_name,
Body: object_content,
ContentEncoding: 'gzip' //important for submitting compressed files
}).promise();
}
我有一个 node.JS 程序可以从 Redis 中读取字符串数组。 我需要将此字符串以 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();
我正在扩展 Jan 为 IBM Cloud Object Storage (COS) 提供的答案:
让我们处理 IBM COS 的凭据:
var config = {
endpoint: 's3.us-south.cloud-object-storage.appdomain.cloud',
apiKeyId: 'put your key here',
serviceInstanceId: 'put serviceInstanceID here',
};
//bucket settings
var evaluation_bucket_name = 'evaluation.data'
现在假设您有一个字符串,其中包含要压缩并提交给 IBM COS 的数据(您的数据可以是 JSON 格式或任何其他格式的用户数据)。这里我有一个名为 aggregated_records
的变量,格式为 JSON。
toBeSubmitted = JSON.stringify(aggregated_records)
const zlib = require('zlib');
const toBeSubmittedCompressed = zlib.gzipSync(toBeSubmitted);
var object_name = 'aggregated_records.json.gzip'
现在,我们将其提交给IBM COS。
CreateObject(evaluation_bucket_name, object_name, toBeSubmittedCompressed)
.then(function () {
console.log(`###The object has been successfully submitted to the IBM Object Storage!`)
})
.catch(function (err) {
console.error('An error occurred:', util.inspect(err))
});
现在,让我们来看看提交功能
function CreateObject(bucket_name, object_name, object_content) {
console.log('Creating object');
return cos.putObject({
Bucket: bucket_name,
Key: object_name,
Body: object_content,
ContentEncoding: 'gzip' //important for submitting compressed files
}).promise();
}