Last active
December 19, 2015 02:58
-
-
Save cjdd3b/5886658 to your computer and use it in GitHub Desktop.
Simple S3 writes in Ruby
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
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