Class: Cliaws::Cli::S3

Inherits:
Thor
  • Object
show all
Defined in:
lib/cliaws/cli/s3.rb

Instance Method Summary collapse

Instance Method Details

#bucketsObject



13
14
15
# File 'lib/cliaws/cli/s3.rb', line 13

def buckets
  puts Cliaws.s3.buckets.map {|bucket| bucket.name}
end

#cat(s3_object) ⇒ Object



167
168
169
170
171
172
# File 'lib/cliaws/cli/s3.rb', line 167

def cat(s3_object)
  print Cliaws.s3.get(s3_object, STDOUT)
  puts
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#create(name) ⇒ Object



20
21
22
# File 'lib/cliaws/cli/s3.rb', line 20

def create(name)
  Cliaws.s3.bucket(name, true)
end

#delete(name) ⇒ Object



30
31
32
33
34
# File 'lib/cliaws/cli/s3.rb', line 30

def delete(name)
  bucket = Cliaws.s3.bucket(name, false)
  exit 1 if bucket.nil?
  bucket.delete(options[:force])
end

#get(s3_object, local_path = nil) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cliaws/cli/s3.rb', line 179

def get(s3_object, local_path=nil)
  out = if local_path then
          File.open(local_path, "wb")
        else
          STDOUT
        end

  Cliaws.s3.get(s3_object, out)
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#grant(thing, who, *permissions) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cliaws/cli/s3.rb', line 219

def grant(thing, who, *permissions)
  who = case who
        when /^AllUsers$/i
          "http://acs.amazonaws.com/groups/global/AllUsers"
        when /^AuthenticatedUsers$/i
          "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
        else
          who
        end
  Cliaws.s3.grant(thing, who => permissions.map {|perm| perm.gsub('-', '_')})
  grants(thing)
end

#grants(thing) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/cliaws/cli/s3.rb', line 201

def grants(thing)
  result = Cliaws.s3.grants(thing).inject(Hash.new) do |memo, grant|
    memo[grant.to_s] = grant.perms
    memo
  end

  result = {thing => result}.to_yaml
  puts result
end

#head(s3_object) ⇒ Object



192
193
194
195
196
# File 'lib/cliaws/cli/s3.rb', line 192

def head(s3_object)
  puts Cliaws.s3.head(s3_object).to_yaml
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#list(prefix = "") ⇒ Object



65
66
67
68
69
# File 'lib/cliaws/cli/s3.rb', line 65

def list(prefix="")
  puts Cliaws.s3.list(prefix)
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#put(*args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cliaws/cli/s3.rb', line 95

def put(*args)
  paths = args
  s3_object = paths.pop

  single_put_mapper = lambda do |source, s3_path|
    raise ArgumentError, "Writing directly from STDIN is forbidden when STDIN's size is unknown.  The RightAws library will write a zero-byte file to Amazon's servers." unless source.respond_to?(:lstat) || source.respond_to?(:size)
    s3_path
  end

  multi_put_mapper  = lambda do |source, s3_path|
    if source.respond_to?(:path) then
      File.join(s3_path, File.basename(source.path))
    else
      raise ArgumentError, "Cannot write to a directory when one or more sources are not files: #{source.inspect}"
    end
  end

  if options[:data] && !paths.empty? then
    raise ArgumentError, "Cannot specify both --data and filename(s) to send."
  elsif options[:data] then
    sources = [StringIO.new(options[:data])]
    mapper  = single_put_mapper
  elsif paths == ["-"] then
    sources = [STDIN]
    mapper  = single_put_mapper
  else
    targets = paths.map {|filename| filename.to_s}
    case targets.length
    when 0
      sources = [STDIN]
      mapper  = single_put_mapper
    when 1
      sources = targets.map {|target| File.open(target, "rb")}
      mapper  = single_put_mapper
    else
      sources = targets.map {|target| File.open(target, "rb")}
      mapper  = multi_put_mapper
    end
  end

  sources.each do |source|
    target = mapper.call(source, s3_object)
    if source.respond_to?(:path) then
      puts "#{source.path} => #{target}"
    else
      puts "STDIN => #{target}"
    end

    Cliaws.s3.put(source, target)
  end
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#revoke(thing, who, *permissions) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cliaws/cli/s3.rb', line 241

def revoke(thing, who, *permissions)
  who = case who
        when /^AllUsers$/i
          "http://acs.amazonaws.com/groups/global/AllUsers"
        when /^AuthenticatedUsers$/i
          "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
        else
          who
        end
  Cliaws.s3.grant(thing, who => permissions.map {|perm| perm.gsub('_', '-')})
  grants(thing)
end

#rm(*paths) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/cliaws/cli/s3.rb', line 154

def rm(*paths)
  paths.each do |path|
    puts "rm #{path}"
    Cliaws.s3.rm(path)
  end
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#touch(s3_object) ⇒ Object



76
77
78
79
80
# File 'lib/cliaws/cli/s3.rb', line 76

def touch(s3_object)
  Cliaws.s3.put("", s3_object)
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end

#url(s3_object) ⇒ Object



39
40
41
42
43
# File 'lib/cliaws/cli/s3.rb', line 39

def url(s3_object)
  puts Cliaws.s3.url(s3_object)
rescue Cliaws::S3::UnknownBucket
  abort "Could not find bucket named #{$!.bucket_name}"
end