Skip to content

Commit

Permalink
Add s3rm command
Browse files Browse the repository at this point in the history
  • Loading branch information
aboisvert committed Oct 18, 2011
1 parent 30d6d89 commit dcb241f
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
6 changes: 6 additions & 0 deletions _s3rm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')

load File.join(File.dirname(__FILE__), 'bin', 's3rm')

3 changes: 3 additions & 0 deletions bin/s3rm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
require 's3cp/s3rm'

132 changes: 132 additions & 0 deletions lib/s3cp/s3rm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require 'rubygems'
require 'extensions/kernel' if RUBY_VERSION =~ /1.8/
require 'right_aws'
require 'optparse'
require 'date'
require 'highline/import'

require 's3cp/utils'

# Parse arguments
options = {}
options[:recursive] = false
options[:include_regex] = nil
options[:exclude_regex] = nil
options[:test] = false
options[:silent] = false
options[:fail_if_not_exist] = false

op = OptionParser.new do |opts|
opts.banner = "s3rm [path]"
opts.separator ''

opts.on("-r", "--recursive", "Delete S3 keys matching provided prefix.") do
options[:recursive] = true
end

opts.on("-i REGEX", "--include REGEX", "Delete only S3 objects matching the following regular expression.") do |regex|
options[:include_regex] = regex
end

opts.on("-x REGEX", "--exclude REGEX", "Do not delete any S3 objects matching provided regular expression.") do |regex|
options[:exclude_regex] = regex
end

opts.on("-F", "--fail-if-not-exist", "Fail if no S3 object match provided key, prefix and/or regex") do
options[:fail_if_not_exist] = true
end

opts.on("-t", "--test", "Only display matching keys; do not actually delete anything.") do
options[:test] = true
end

opts.on("--silent", "Do not display keys as they are deleted.") do
options[:silent] = true
end

opts.on("--verbose", "Verbose mode") do
options[:verbose] = true
end

opts.on_tail("-h", "--help", "Show this message") do
puts op
exit
end
end
op.parse!(ARGV)

unless ARGV.size > 0
puts op
exit(1)
end

if options[:include_regex] && !options[:recursive]
puts "-i (--include regex) option requires -r (recursive) option."
exit(1)
end

if options[:exclude_regex] && !options[:recursive]
puts "-x (--exclude regex) option requires -r (recursive) option."
exit(1)
end

url = ARGV[0]

if options[:verbose]
puts "URL: #{url}"
puts "Options: #{options.inspect}"
end

@bucket, @key = S3CP.bucket_and_key(url)
fail "Your URL looks funny, doesn't it?" unless @bucket

if options[:verbose]
puts "bucket #{@bucket}"
puts "key #{@key}"
end

include_regex = options[:include_regex] ? Regexp.new(options[:include_regex]) : nil
exclude_regex = options[:exclude_regex] ? Regexp.new(options[:exclude_regex]) : nil

@s3 = S3CP.connect()

if options[:recursive]
total_matching = 0

@s3.interface.incrementally_list_bucket(@bucket, :prefix => @key) do |page|
page[:contents].each do |entry|
key = "s3://#{@bucket}/#{entry[:key]}"

matching = true
matching = false if include_regex && !include_regex.match(entry[:key])
matching = false if exclude_regex && exclude_regex.match(entry[:key])

puts "#{key} => #{matching}" if options[:verbose]

if matching
total_matching += 1
puts key unless options[:silent] || options[:verbose]
@s3.interface.delete(@bucket, entry[:key]) unless options[:test]
end
end
end

if options[:fail_if_not_exist] && total_matching == 0
puts "No matching keys."
exit(1)
end
else
# delete a single file; check if it exists
if options[:fail_if_not_exist] && @s3.interface.head(@bucket, @key) == nil
key = "s3://#{@bucket}/#{@key}"
puts "#{key} does not exist."
exit(1)
end

begin
@s3.interface.delete(@bucket, @key) unless options[:test]
rescue => e
puts e.to_s
raise e unless e.to_s =~ /Not Found/
end
end
1 change: 1 addition & 0 deletions s3cp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Gem::Specification.new do |s|
s.executables << 's3cp'
s.executables << 's3cat'
s.executables << 's3mod'
s.executables << 's3rm'

s.extra_rdoc_files = ['README.md', 'History.txt']

Expand Down

0 comments on commit dcb241f

Please sign in to comment.