Skip to content

Instantly share code, notes, and snippets.

@mschulkind
Created March 20, 2012 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschulkind/2142024 to your computer and use it in GitHub Desktop.
Save mschulkind/2142024 to your computer and use it in GitHub Desktop.
BZip2 and store the contents of a local zip file on S3
def establish_s3_connection!
AWS::S3::Base.establish_connection!(
access_key_id: ENV['AMAZON_ACCESS_KEY_ID'],
secret_access_key: ENV['AMAZON_SECRET_ACCESS_KEY']
)
end
establish_s3_connection!
local_file_name = 'bar/foo.zip'
bucket = 'my-upload-bucket'
s3_prefix = 'upload/'
zip = Zip::ZipInputStream.open(local_file_name)
prefix = "#{s3_prefix}"
while (entry = zip.get_next_entry)
puts "Reading '#{entry.name}' from zip..."
data = entry.get_input_stream.read
puts "Compressing..."
compressor = Bzip2::Writer.new
compressor << data
compressed_data = compressor.flush
path = File.join(prefix, "#{entry.name}.bz2")
puts "Storing '#{path}' on S3..."
AWS::S3::S3Object.store(path, compressed_data, bucket)
end
puts "Done uploading to S3."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment