Skip to content

Instantly share code, notes, and snippets.

@cjdd3b
Last active December 19, 2015 02:58
Show Gist options
  • Save cjdd3b/5886658 to your computer and use it in GitHub Desktop.
Save cjdd3b/5886658 to your computer and use it in GitHub Desktop.
Simple S3 writes in Ruby
AMAZON_ACCESS_KEY = 'WHATEVER'
AMAZON_SECRET_KEY = 'SECRET_WHATEVER'
# I'm old-school, so I like the AWS-S3 gem. It's just a lightweight wrapper around Amazon's API.
# https://github.com/marcel/aws-s3
require "aws/s3"
include AWS::S3
def publish_json!(bucket='int.nyt.com', path='applications/represent-json/', filename='foo.json')
some_json = "{'title': 'Hi, my name is JSON!'}"
# Create AWS connection
Base.establish_connection!(
:access_key_id => AMAZON_ACCESS_KEY,
:secret_access_key => AMAZON_SECRET_KEY
)
# Store the file in S3
S3Object.store(path + filename,
some_json, # JSON string to upload goes here
bucket,
:content_type => 'application/json', # This = important
:cache_control => 'public,max-age=300') # 5-minute TTL, but you can go shorter
end
publish_json!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment