Last active
August 29, 2015 13:57
-
-
Save paulofierro/9588649 to your computer and use it in GitHub Desktop.
Compressing JSON on S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'zlib' | |
require 'stringio' | |
# Connect to the bucket and create the file | |
s3 = AWS::S3.new(access_key_id: 'S3_ACCESS_KEY', secret_access_key: 'S3_SECRET_KEY', region: 'S3_REGION') | |
bucket = s3.buckets['S3_BUCKET'] | |
file = bucket.objects.create('S3_FILE_NAME', '') | |
# Set the content type and encoding. This is the important bit | |
options = {:acl => :public_read, :content_type => 'application/json', :content_encoding => 'gzip'} | |
# Create the data we're going to write | |
json = MultiJson.encode({:status => 'ok', :data => []}, :pretty => false) | |
# Finally write the file | |
file.write(gzip(json), options) | |
# From http://code-dojo.blogspot.com/2012/10/gzip-compressiondecompression-in-ruby.html | |
def gzip(string) | |
wio = StringIO.new("w") | |
w_gz = Zlib::GzipWriter.new(wio) | |
w_gz.write(string) | |
w_gz.close | |
compressed = wio.string | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment