Skip to content

Instantly share code, notes, and snippets.

@yamayo
Last active January 10, 2019 19:29
Show Gist options
  • Save yamayo/7981897 to your computer and use it in GitHub Desktop.
Save yamayo/7981897 to your computer and use it in GitHub Desktop.
AWS S3 upload/download with gzip
# A sample Gemfile
source 'https://rubygems.org'
gem 'aws-sdk'
gem 'pit'
require 'pit'
require 'aws-sdk'
ENV['EDITOR'] ||= 'vim'
config = Pit.get('s3_gzipper', require: {
access_key_id: '',
secret_access_key: '',
bucket_name: ''
})
def dummy_filename
"/tmp/s3-gzip-dummy"
end
def gzip(data)
sio = StringIO.new
gz = Zlib::GzipWriter.new(sio)
gz.write(data)
gz.close
sio.string
end
def gunzip(data)
sio = StringIO.new(data)
gz = Zlib::GzipReader.new(sio)
read_data = gz.read
gz.close
read_data
end
`dd if=/dev/zero of=#{ dummy_filename } count=1 bs=150k`
s3 = AWS::S3.new(config)
object = s3.buckets[config[:bucket_name]].objects['gzip_test']
data = File.read(dummy_filename)
# Write
object.write(gzip(data), { acl: :public_read, content_type: 'application/json; charset=UTF-8', content_encoding: 'gzip' })
# Read
p gunzip(object.read)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment