Class: S3Utils::Main

Inherits:
Thor
  • Object
show all
Defined in:
lib/s3utils.rb

Instance Method Summary collapse

Instance Method Details

#copy(from, to) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/s3utils.rb', line 106

def copy(from, to)
  with_error_handling do 
    abort "Error: incorrect bucket:key format" unless from =~ /(.+):(.+)/ &&  to =~ /(.+):(.+)/
    from =~ /(.+):(.+)/
    o_from = s3.buckets[$1].objects[$2]
    raise "Object #{from} does not exist" unless o_from.exists?

    to =~ /(.+):(.+)/
    o_to = s3.buckets[$1].objects.create($2, :data => o_from.read)
  end
end

#createbucket(name) ⇒ Object



25
26
27
28
29
# File 'lib/s3utils.rb', line 25

def createbucket(name)
  with_error_handling do
    bucket = s3.buckets.create(name)
  end
end

#deletebucket(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/s3utils.rb', line 52

def deletebucket(name)
  with_error_handling do
    if options.force?
      objects =  s3.buckets[name].objects
      objects.each do |o|
        o.delete
      end
    end
    s3.buckets[name].delete
  end
end

#deletekey(bucket_key) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/s3utils.rb', line 34

def deletekey(bucket_key)
  with_error_handling do
    abort "Error: incorrect bucket:key format" unless bucket_key =~ /(.+):(.+)/
    if options.prefix?
      objects =  s3.buckets[$1].objects.with_prefix($2)
      objects.each do |o|
        o.delete
      end
    else
      s3.buckets[$1].objects[$2].delete
    end
  end
end

#get(bucket_key, file) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/s3utils.rb', line 89

def get(bucket_key, file)
  with_error_handling do
    abort "Error: incorrect bucket:key format" unless bucket_key =~ /(.+):(.+)/
    o = s3.buckets[$1].objects[$2]
    File.open(file, "w"){|f| f.write(o.read)}
  end
end

#get_timestamp(bucket_key) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/s3utils.rb', line 80

def get_timestamp(bucket_key)
  with_error_handling do
    abort "Error: incorrect bucket:key format" unless bucket_key =~ /(.+):(.+)/
    o = s3.buckets[$1].objects[$2]
    puts o.last_modified.to_i
  end
end

#list(bucket_prefix) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/s3utils.rb', line 66

def list(bucket_prefix)
  with_error_handling do
    if  bucket_prefix =~ /(.+):(.+)/
      objects =  s3.buckets[$1].objects.with_prefix($2)
    else
      objects =  s3.buckets[bucket_prefix].objects
    end
    objects.each do |o|
      puts o.key
    end
  end
end

#listbucketsObject



15
16
17
18
19
20
21
22
# File 'lib/s3utils.rb', line 15

def listbuckets
  with_error_handling do
    s3.buckets.each do |bucket|
      puts bucket.name
    end
  end

end

#move(from, to) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/s3utils.rb', line 120

def move(from, to)
  with_error_handling do 
    abort "Error: incorrect bucket:key format" unless from =~ /(.+):(.+)/ &&  to =~ /(.+):(.+)/
    from =~ /(.+):(.+)/
    o_from = s3.buckets[$1].objects[$2]
    raise "Object #{from} does not exist" unless o_from.exists?

    to =~ /(.+):(.+)/
    o_to = s3.buckets[$1].objects.create($2, :data => o_from.read)
    o_from.delete
  end
end

#put(bucket_key, file) ⇒ Object



98
99
100
101
102
103
# File 'lib/s3utils.rb', line 98

def put(bucket_key, file)
  with_error_handling do
    abort "Error: incorrect bucket:key format" unless bucket_key =~ /(.+):(.+)/
    File.open(file, "r") { |f| s3.buckets[$1].objects.create($2, :data => f.read)}
  end
end

#versionObject



11
12
13
# File 'lib/s3utils.rb', line 11

def version
  puts S3Utils::VERSION
end