Skip to content

Instantly share code, notes, and snippets.

@bboure
Created April 2, 2021 19:53
Show Gist options
  • Save bboure/d5fb91c0aa40db56909c7c7ac9f9df59 to your computer and use it in GitHub Desktop.
Save bboure/d5fb91c0aa40db56909c7c7ac9f9df59 to your computer and use it in GitHub Desktop.
DynamoDB Compress large items
//read.js
const AWS = require('aws-sdk');
const { gunzipSync } = require('zlib');
AWS.config.update({ region: 'eu-west-1' });
const dynamoDbClient = new AWS.DynamoDB();
dynamoDbClient.getItem({
"TableName": "blog",
"ReturnConsumedCapacity": "TOTAL",
"Key": {
"author": {
"S": "bboure"
},
"slug": {
"S": "raw-blog-post"
},
}
}).promise().then(result => {
console.log('Read capacity for raw post', result.ConsumedCapacity );
});
dynamoDbClient.getItem({
"TableName": "blog",
"ReturnConsumedCapacity": "TOTAL",
"Key": {
"author": {
"S": "bboure"
},
"slug": {
"S": "compressed-blog-post"
},
}
}).promise().then(result => {
console.log('Read capacity for compressed post', result.ConsumedCapacity );
// uncompress post content
const content = gunzipSync(result.Item.content.B).toString();
console.log(`Original text with ${content.length} characters and ${content.split(' ').length} words`);
});
//write.js
const AWS = require('aws-sdk');
const { loremIpsum } = require('lorem-ipsum');
const { gzipSync } = require('zlib');
const content = loremIpsum({
count: 20,
units: "paragraph",
format: "plain",
paragraphLowerBound: 5,
paragraphUpperBound: 15,
sentenceLowerBound: 5,
sentenceUpperBound: 15,
suffix: "\n\n\n",
});
// output some stats about the text's lenght.
console.log(`Generated a text with ${content.length} characters and ${content.split(' ').length} words`);
// compress the content
const compressed = gzipSync(content);
// more stats about the content
console.log(`total size (uncompressed): ~${Math.round(content.length/1024)} KB`);
console.log(`total size (compressed): ~${Math.round(compressed.length/1024)} KB`);
// config DynamoDB
AWS.config.update({ region: 'eu-west-1' });
const dynamoDbClient = new AWS.DynamoDB();
dynamoDbClient.putItem({
"TableName": "blog",
"ReturnConsumedCapacity": "TOTAL",
"Item": {
"author": {
"S": "bboure"
},
"slug": {
"S": "raw-blog-post"
},
"title": {
"S": "My blog post"
},
"content": {
"S": content,
}
}
}).promise().then(result => {
console.log('Write capacity for raw post', result.ConsumedCapacity );
});
dynamoDbClient.putItem({
"TableName": "blog",
"ReturnConsumedCapacity": "TOTAL",
"Item": {
"author": {
"S": "bboure"
},
"slug": {
"S": "compressed-blog-post"
},
"title": {
"S": "My blog post"
},
"content": {
"B": compressed,
}
}
}).promise().then(result => {
console.log('Write capacity for compressed post', result.ConsumedCapacity );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment