Skip to content

Instantly share code, notes, and snippets.

@aloon
Created March 17, 2012 00:43
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 aloon/2053913 to your computer and use it in GitHub Desktop.
Save aloon/2053913 to your computer and use it in GitHub Desktop.
download and delete all from S3
#!/usr/bin/env ruby
require 'fileutils'
require 'right_aws'
AWS_ACCESS_KEY_ID = '...'
AWS_SECRET_ACCESS_KEY = '...'
BUCKET = 'jujuego'
DOWNLOAD_DIR = '../files'
BUCKET_NAME = 'jujuego'
NTHREADS = 50
TIMEOUT = 10
class Hilo
attr_accessor :iniciado, :hilo
def initialize
@iniciado = Time.now
end
end
class Down
def initialize
@hilos = Array.new
for i in 0..NTHREADS do
@hilos[i]=nil
end
@bucket = RightAws::S3.new(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY).bucket(BUCKET_NAME)
end
def find_hilo
while true do
i = 0
@hilos.each{|h|
return i if h.nil? || h.hilo.nil? || !h.hilo.alive?
i = i +1
}
sleep 0.2
end
end
def esperar_hilos
i = 0
@hilos.each{|h|
if !h.nil? && h.hilo.alive?
if Time.now.to_i > @hilos[i].iniciado.to_i + TIMEOUT
Thread.kill @hilos[i].hilo
else
h.hilo.join TIMEOUT
end
end
i += 1
}
end
def execute
cont = true
while cont do
puts '+++++++++++++++++++'
puts '+++++++++++++++++++'
puts 'obteniendo keys'
puts '+++++++++++++++++++'
puts '+++++++++++++++++++'
keys = @bucket.keys('max-keys' => 2000)
cont = false if keys.size == 0
keys.each{ |obj|
p obj.name
if obj.name[-12,12][0,8] == 'original'
descargar obj
else
borrar obj
end
}
esperar_hilos
end
end
def borrar obj
h = find_hilo
@hilos[h]=Hilo.new
@hilos[h].hilo = Thread.new(obj) do |obj|
begin
obj.delete
puts 'borrado'
rescue Exception => error
puts 'error borrando'
p error
end
end
end
def descargar obj
h = find_hilo
@hilos[h]=Hilo.new
@hilos[h].hilo = Thread.new(obj) do |obj|
begin
file = Dir.pwd + '/' + DOWNLOAD_DIR + '/' + BUCKET_NAME + '/' + obj.name
FileUtils.mkdir_p File.dirname(file)
myfile = File.open(file,'w')
myfile.puts @bucket.get(obj.name)
obj.delete
puts 'descargado'
rescue Exception => error
puts 'error descargando'
p error
end
end
end
end
Down.new.execute
puts 'fin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment