Class: Cliaws::Cli::S3

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

Instance Method Summary collapse

Instance Method Details

#bucketsObject



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

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

#cat(s3_object) ⇒ Object



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

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



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

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

#delete(name) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#put(*args) ⇒ Object



94
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
# File 'lib/cliaws/cli/s3.rb', line 94

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



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

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



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

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



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

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



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

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