Skip to content

Instantly share code, notes, and snippets.

@gilmation
Created July 7, 2010 09:58
Show Gist options
  • Save gilmation/466514 to your computer and use it in GitHub Desktop.
Save gilmation/466514 to your computer and use it in GitHub Desktop.
require 'yaml'
require 'right_aws'
require 'fileutils'
#
# Utility methods for accessing and manipulating
# Amazon s3 buckets
#
class S3 < Thor
include FileUtils::Verbose
# List the contents of the given bucket
# @param bucket_name [String] the bucket where the information is to be stored
desc("list_contents_bucket", "List the contents of this bucket in the current users S3 repo")
def list_contents_bucket(bucket_name)
# create the bucket - if it doesn't already exist
remote_bucket = RightAws::S3::Bucket.create(connect, "#{bucket_name}", true)
# have a look at the keys that are already present
remote_bucket.keys.each { |remote_key| puts "Bucket [#{bucket_name}] contains key [#{remote_key}]" }
end
# List the available buckets
desc("list_available_buckets", "List the available buckets in the current users S3 repo")
def list_available_buckets
# get the available buckets
# then list them
connect.buckets.each { |bucket| puts "Bucket [#{bucket.name}]" }
end
private
# Connect to S3. The configuration
# of S3 is contained in the users ~/.aws/aws_config.yml file.
def connect
# load the config
config = YAML.load_file(File.join(ENV['HOME'], '.aws', 'aws_config.yml'))
# Sanity check the config
throw "Cannot connect with an empty configuration Hash" if config.empty?
return RightAws::S3.new(config['access_key_id'], config['secret_access_key'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment