Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanVaughan/cb23c037b6db06515648 to your computer and use it in GitHub Desktop.
Save IanVaughan/cb23c037b6db06515648 to your computer and use it in GitHub Desktop.
Uploads a given file to S3 files.econultancy.com bucket and sets permissions to Everyone:Read/Download
#!/usr/bin/env ruby
# Uploads a given file to S3 files.econultancy.com bucket
# and sets permissions to Everyone:Read/Download
# (Default of Tech:Read/Write/Delete still set)
# Uses config in ~/.fog if present,
# otherwise falls back to using ENV vars
# export AWS_ACCESS_KEY_ID=xxx
# export AWS_SECRET_ACCESS_KEY=yyy
# Usage :
# ruby upload_to_s3.rb filename_to_upload
require 'aws-sdk'
require 'yaml'
bucket_name = 'files.econsultancy.com'
file_name = ARGV[0]
def fog_file
File.join(Dir.home, '.fog')
end
def fog_config
if File.exist?(fog_file)
config = YAML.load_file(fog_file)
if config.key?('econsultancy')
config['econsultancy']
end
end
end
if config = fog_config
AWS.config(access_key_id: config['aws_access_key_id'], secret_access_key: config['aws_secret_access_key'])
end
# Get an instance of the S3 interface.
s3 = AWS::S3.new
# Upload a file.
key = File.basename(file_name)
s3.buckets[bucket_name].objects[key].write(file: file_name, acl: :public_read)
puts "Uploading file #{file_name} to bucket #{bucket_name}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment