Skip to content

Instantly share code, notes, and snippets.

@zackfern
Last active December 14, 2015 11:19
Show Gist options
  • Save zackfern/5078723 to your computer and use it in GitHub Desktop.
Save zackfern/5078723 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# README
# This is a simple script to upload files to Amazon S3.
# Configuration is provided below for your S3 credentials, bucket to upload to, path to upload to, and base URL.
# When the script is finished, it will output the base URL followed by the path and uploaded file name.
# The script can rename files if USE_ORIGINAL_FILE_NAME is set to false.
# By default, it will name the file based off the current date and time (for example, 2013-03-03-153427.png).
#
# Once you've configured the script, simply pass the script the path to your file.
#
# Example:
# ruby ./ferncloud.rb /Users/zackfern/Desktop/Screen Shot 2013-03-03 at 3.34.18 PM.png
#
# For more information about the origin of the script, and a blog post about it, visit: http://zackfern.me/2013/03/03/ghetto-cloud-returns.html
#
# LICENSE (MIT)
# Copyright (c) 2013 Zack Fernandes
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# CONFIGURATION
#
# Your S3 Access Key and Secret Access Key
ACCESS_KEY = 'DO THE D.A.N.C.E.'
SECRET_KEY = '1 2 3 4 5'
#
# The name of the S3 Bucket you want to upload to
MY_BUCKET = 's3.zackfern.me'
#
# The path you'd like to upload to. NO TRAILING SLASHES!
MY_PATH = 's'
#
# The base URL for your copied file upload
BASE_URL = 'http://s3.zackfern.me'
#
# Do you want to use the original file name, or modify it before it's uploaded?
USE_ORIGINAL_FILE_NAME = false
#
# You shouldn't change anything below this line unless you know what you're doing.
require 'rubygems'
require 'aws/s3'
class FernCloud
def initialize
if ARGV.empty?
# Nothing was passed to the script.
return false
else
@file_path = ARGV.join(' ')
connect
upload
end
end
def url
"#{BASE_URL}/#{MY_PATH}/#{@file_name}"
end
private
# Private: Connect to Amazon S3
def connect
AWS::S3::Base.establish_connection!(
:access_key_id => ACCESS_KEY,
:secret_access_key => SECRET_KEY
)
end
# Private: Upload the provded file to Amazon S3 in the specified bucket and path
def upload
get_file_name
AWS::S3::S3Object.store "#{MY_PATH}/#{@file_name}", open(@file_path), MY_BUCKET, access: :public_read
end
# Private: Retreieve the File Name from the File Path
def get_file_name
if USE_ORIGINAL_FILE_NAME
@file_name = File.basename(@file_path)
else
# This is where you can change the uploaded file name to be whatever you want.
# I prefer using the date and time, but you can let your imagination run wild.
# Just make sure you append the proper extension from the @file_path
current_date_time, file_extension = Time.new.strftime("%F-%H%M%S"), File.extname(@file_path)
@file_name = current_date_time + file_extension
end
end
end
# Run this bitch.
my_upload = FernCloud.new
puts my_upload.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment