Skip to content

Instantly share code, notes, and snippets.

@alexcasalboni
Created June 17, 2019 16:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexcasalboni/c3418d148e44866982957a3f3902a2a0 to your computer and use it in GitHub Desktop.
Save alexcasalboni/c3418d148e44866982957a3f3902a2a0 to your computer and use it in GitHub Desktop.
How to decode CloudWatch Logs events
const zlib = require('zlib');
exports.decode = async (data) => {
const compressedPayload = Buffer.from(data, 'base64');
const jsonPayload = zlib.gunzipSync(compressedPayload).toString('utf8');
return JSON.parse(jsonPayload);
}
import zlib
import json
from base64 import b64decode
def decode(data):
compressed_payload = b64decode(data)
json_payload = zlib.decompress(compressed_payload, 16+zlib.MAX_WBITS)
return json.loads(json_payload)
@LarryMartell
Copy link

Thanks for this Alex. Do you have a function that is the inverse of this? One that takes a dict and creates the compressed encoded payload? I need something like that for my unit tests.

@alexcasalboni
Copy link
Author

@LarryMartell it should be fairly easy to invert the semantics and order of all operations :)

Something like this (untested):

import zlib
import json
from base64 import b64encode

def encode(data):
    json_payload = json.dumps(data)
    compressed_payload = zlib.compress(json_payload)
    return b64encode(compressed_payload)   

@LarryMartell
Copy link

LarryMartell commented Jun 24, 2021

Thanks for the reply. I ended up having to do this to get it to work:

def encode(data):
    json_payload = json.dumps(data)
    compressed_payload = mycompress(bytes(json_payload, 'utf-8'))
    return b64encode(compressed_payload)


def mycompress(data):
    obj = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
    result = obj.compress(data)
    result += obj.flush()
    return result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment