aws 1.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +246 -0
- data/Manifest.txt +19 -0
- data/README.txt +168 -0
- data/Rakefile +17 -0
- data/lib/acf/right_acf_interface.rb +379 -0
- data/lib/awsbase/benchmark_fix.rb +39 -0
- data/lib/awsbase/right_awsbase.rb +797 -0
- data/lib/awsbase/support.rb +111 -0
- data/lib/ec2/right_ec2.rb +1737 -0
- data/lib/right_aws.rb +69 -0
- data/lib/s3/right_s3.rb +1094 -0
- data/lib/s3/right_s3_interface.rb +1175 -0
- data/lib/sdb/active_sdb.rb +936 -0
- data/lib/sdb/right_sdb_interface.rb +701 -0
- data/lib/sqs/right_sqs.rb +388 -0
- data/lib/sqs/right_sqs_gen2.rb +286 -0
- data/lib/sqs/right_sqs_gen2_interface.rb +444 -0
- data/lib/sqs/right_sqs_interface.rb +596 -0
- metadata +96 -0
data/lib/right_aws.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2007-2008 RightScale Inc
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#
|
23
|
+
|
24
|
+
require 'benchmark'
|
25
|
+
require 'net/https'
|
26
|
+
require 'uri'
|
27
|
+
require 'time'
|
28
|
+
require "cgi"
|
29
|
+
require "base64"
|
30
|
+
require "rexml/document"
|
31
|
+
require "openssl"
|
32
|
+
require "digest/sha1"
|
33
|
+
|
34
|
+
require 'rubygems'
|
35
|
+
require 'right_http_connection'
|
36
|
+
|
37
|
+
$:.unshift(File.dirname(__FILE__))
|
38
|
+
require 'awsbase/benchmark_fix'
|
39
|
+
require 'awsbase/support'
|
40
|
+
require 'awsbase/right_awsbase'
|
41
|
+
require 'ec2/right_ec2'
|
42
|
+
require 's3/right_s3_interface'
|
43
|
+
require 's3/right_s3'
|
44
|
+
require 'sqs/right_sqs_interface'
|
45
|
+
require 'sqs/right_sqs'
|
46
|
+
require 'sqs/right_sqs_gen2_interface'
|
47
|
+
require 'sqs/right_sqs_gen2'
|
48
|
+
require 'sdb/right_sdb_interface'
|
49
|
+
require 'acf/right_acf_interface'
|
50
|
+
|
51
|
+
|
52
|
+
module RightAws #:nodoc:
|
53
|
+
module VERSION #:nodoc:
|
54
|
+
MAJOR = 1
|
55
|
+
MINOR = 10
|
56
|
+
TINY = 1
|
57
|
+
|
58
|
+
STRING = [MAJOR, MINOR, TINY].join('.')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
#-
|
63
|
+
|
64
|
+
# We also want everything available in the Rightscale namespace for backward
|
65
|
+
# compatibility reasons.
|
66
|
+
module Rightscale #:nodoc:
|
67
|
+
include RightAws
|
68
|
+
extend RightAws
|
69
|
+
end
|
data/lib/s3/right_s3.rb
ADDED
@@ -0,0 +1,1094 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2007-2008 RightScale Inc
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#
|
23
|
+
|
24
|
+
module RightAws
|
25
|
+
|
26
|
+
# = RightAws::S3 -- RightScale's Amazon S3 interface
|
27
|
+
# The RightAws::S3 class provides a complete interface to Amazon's Simple
|
28
|
+
# Storage Service.
|
29
|
+
# For explanations of the semantics
|
30
|
+
# of each call, please refer to Amazon's documentation at
|
31
|
+
# http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=48
|
32
|
+
#
|
33
|
+
# See examples below for the bucket and buckets methods.
|
34
|
+
#
|
35
|
+
# Error handling: all operations raise an RightAws::AwsError in case
|
36
|
+
# of problems. Note that transient errors are automatically retried.
|
37
|
+
#
|
38
|
+
# It is a good way to use domain naming style getting a name for the buckets.
|
39
|
+
# See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingBucket.html
|
40
|
+
# about the naming convention for the buckets. This case they can be accessed using a virtual domains.
|
41
|
+
#
|
42
|
+
# Let assume you have 3 buckets: 'awesome-bucket', 'awesome_bucket' and 'AWEsomE-bucket'.
|
43
|
+
# The first ones objects can be accessed as: http:// awesome-bucket.s3.amazonaws.com/key/object
|
44
|
+
#
|
45
|
+
# But the rest have to be accessed as:
|
46
|
+
# http:// s3.amazonaws.com/awesome_bucket/key/object and http:// s3.amazonaws.com/AWEsomE-bucket/key/object
|
47
|
+
#
|
48
|
+
# See: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/VirtualHosting.html for better explanation.
|
49
|
+
#
|
50
|
+
class S3
|
51
|
+
attr_reader :interface
|
52
|
+
|
53
|
+
# Create a new handle to an S3 account. All handles share the same per process or per thread
|
54
|
+
# HTTP connection to Amazon S3. Each handle is for a specific account.
|
55
|
+
# The +params+ are passed through as-is to RightAws::S3Interface.new
|
56
|
+
#
|
57
|
+
# Params is a hash:
|
58
|
+
#
|
59
|
+
# {:server => 's3.amazonaws.com' # Amazon service host: 's3.amazonaws.com'(default)
|
60
|
+
# :port => 443 # Amazon service port: 80 or 443(default)
|
61
|
+
# :protocol => 'https' # Amazon service protocol: 'http' or 'https'(default)
|
62
|
+
# :multi_thread => true|false # Multi-threaded (connection per each thread): true or false(default)
|
63
|
+
# :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
|
64
|
+
def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
|
65
|
+
@interface = S3Interface.new(aws_access_key_id, aws_secret_access_key, params)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Retrieve a list of buckets.
|
69
|
+
# Returns an array of RightAws::S3::Bucket instances.
|
70
|
+
# # Create handle to S3 account
|
71
|
+
# s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
|
72
|
+
# my_buckets_names = s3.buckets.map{|b| b.name}
|
73
|
+
# puts "Buckets on S3: #{my_bucket_names.join(', ')}"
|
74
|
+
def buckets
|
75
|
+
@interface.list_all_my_buckets.map! do |entry|
|
76
|
+
owner = Owner.new(entry[:owner_id], entry[:owner_display_name])
|
77
|
+
Bucket.new(self, entry[:name], entry[:creation_date], owner)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Retrieve an individual bucket.
|
82
|
+
# If the bucket does not exist and +create+ is set, a new bucket
|
83
|
+
# is created on S3. Launching this method with +create+=+true+ may
|
84
|
+
# affect on the bucket's ACL if the bucket already exists.
|
85
|
+
# Returns a RightAws::S3::Bucket instance or +nil+ if the bucket does not exist
|
86
|
+
# and +create+ is not set.
|
87
|
+
#
|
88
|
+
# s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
|
89
|
+
# bucket1 = s3.bucket('my_awesome_bucket_1')
|
90
|
+
# bucket1.keys #=> exception here if the bucket does not exists
|
91
|
+
# ...
|
92
|
+
# bucket2 = s3.bucket('my_awesome_bucket_2', true)
|
93
|
+
# bucket2.keys #=> list of keys
|
94
|
+
# # create a bucket at the European location with public read access
|
95
|
+
# bucket3 = s3.bucket('my-awesome-bucket-3', true, 'public-read', :location => :eu)
|
96
|
+
#
|
97
|
+
# see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html
|
98
|
+
# (section: Canned Access Policies)
|
99
|
+
#
|
100
|
+
def bucket(name, create=false, perms=nil, headers={})
|
101
|
+
headers['x-amz-acl'] = perms if perms
|
102
|
+
@interface.create_bucket(name, headers) if create
|
103
|
+
buckets.each { |bucket| return bucket if bucket.name == name }
|
104
|
+
nil
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
class Bucket
|
109
|
+
attr_reader :s3, :name, :owner, :creation_date
|
110
|
+
|
111
|
+
# Create a Bucket instance.
|
112
|
+
# If the bucket does not exist and +create+ is set, a new bucket
|
113
|
+
# is created on S3. Launching this method with +create+=+true+ may
|
114
|
+
# affect on the bucket's ACL if the bucket already exists.
|
115
|
+
# Returns Bucket instance or +nil+ if the bucket does not exist
|
116
|
+
# and +create+ is not set.
|
117
|
+
#
|
118
|
+
# s3 = RightAws::S3.new(aws_access_key_id, aws_secret_access_key)
|
119
|
+
# ...
|
120
|
+
# bucket1 = RightAws::S3::Bucket.create(s3, 'my_awesome_bucket_1')
|
121
|
+
# bucket1.keys #=> exception here if the bucket does not exists
|
122
|
+
# ...
|
123
|
+
# bucket2 = RightAws::S3::Bucket.create(s3, 'my_awesome_bucket_2', true)
|
124
|
+
# bucket2.keys #=> list of keys
|
125
|
+
# # create a bucket at the European location with public read access
|
126
|
+
# bucket3 = RightAws::S3::Bucket.create(s3,'my-awesome-bucket-3', true, 'public-read', :location => :eu)
|
127
|
+
#
|
128
|
+
# see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html
|
129
|
+
# (section: Canned Access Policies)
|
130
|
+
#
|
131
|
+
def self.create(s3, name, create=false, perms=nil, headers={})
|
132
|
+
s3.bucket(name, create, perms, headers)
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# Create a bucket instance. In normal use this method should
|
137
|
+
# not be called directly.
|
138
|
+
# Use RightAws::S3::Bucket.create or RightAws::S3.bucket instead.
|
139
|
+
def initialize(s3, name, creation_date=nil, owner=nil)
|
140
|
+
@s3 = s3
|
141
|
+
@name = name
|
142
|
+
@owner = owner
|
143
|
+
@creation_date = creation_date
|
144
|
+
if @creation_date && !@creation_date.is_a?(Time)
|
145
|
+
@creation_date = Time.parse(@creation_date)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Return bucket name as a String.
|
150
|
+
#
|
151
|
+
# bucket = RightAws::S3.bucket('my_awesome_bucket')
|
152
|
+
# puts bucket #=> 'my_awesome_bucket'
|
153
|
+
#
|
154
|
+
def to_s
|
155
|
+
@name.to_s
|
156
|
+
end
|
157
|
+
alias_method :full_name, :to_s
|
158
|
+
|
159
|
+
# Return a public link to bucket.
|
160
|
+
#
|
161
|
+
# bucket.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket'
|
162
|
+
#
|
163
|
+
def public_link
|
164
|
+
params = @s3.interface.params
|
165
|
+
"#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name}"
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns the bucket location
|
169
|
+
def location
|
170
|
+
@location ||= @s3.interface.bucket_location(@name)
|
171
|
+
end
|
172
|
+
|
173
|
+
# Retrieves the logging configuration for a bucket.
|
174
|
+
# Returns a hash of {:enabled, :targetbucket, :targetprefix}
|
175
|
+
#
|
176
|
+
# bucket.logging_info()
|
177
|
+
# => {:enabled=>true, :targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/"}
|
178
|
+
def logging_info
|
179
|
+
@s3.interface.get_logging_parse(:bucket => @name)
|
180
|
+
end
|
181
|
+
|
182
|
+
# Enables S3 server access logging on a bucket. The target bucket must have been properly configured to receive server
|
183
|
+
# access logs.
|
184
|
+
# Params:
|
185
|
+
# :targetbucket - either the target bucket object or the name of the target bucket
|
186
|
+
# :targetprefix - the prefix under which all logs should be stored
|
187
|
+
#
|
188
|
+
# bucket.enable_logging(:targetbucket=>"mylogbucket", :targetprefix=>"loggylogs/")
|
189
|
+
# => true
|
190
|
+
def enable_logging(params)
|
191
|
+
AwsUtils.mandatory_arguments([:targetbucket, :targetprefix], params)
|
192
|
+
AwsUtils.allow_only([:targetbucket, :targetprefix], params)
|
193
|
+
xmldoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><LoggingEnabled><TargetBucket>#{params[:targetbucket]}</TargetBucket><TargetPrefix>#{params[:targetprefix]}</TargetPrefix></LoggingEnabled></BucketLoggingStatus>"
|
194
|
+
@s3.interface.put_logging(:bucket => @name, :xmldoc => xmldoc)
|
195
|
+
end
|
196
|
+
|
197
|
+
# Disables S3 server access logging on a bucket. Takes no arguments.
|
198
|
+
def disable_logging
|
199
|
+
xmldoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><BucketLoggingStatus xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"></BucketLoggingStatus>"
|
200
|
+
@s3.interface.put_logging(:bucket => @name, :xmldoc => xmldoc)
|
201
|
+
end
|
202
|
+
|
203
|
+
# Retrieve a group of keys from Amazon.
|
204
|
+
# +options+ is a hash: { 'prefix'=>'', 'marker'=>'', 'max-keys'=>5, 'delimiter'=>'' }).
|
205
|
+
# Retrieves meta-headers information if +head+ it +true+.
|
206
|
+
# Returns an array of Key instances.
|
207
|
+
#
|
208
|
+
# bucket.keys #=> # returns all keys from bucket
|
209
|
+
# bucket.keys('prefix' => 'logs') #=> # returns all keys that starts with 'logs'
|
210
|
+
#
|
211
|
+
def keys(options={}, head=false)
|
212
|
+
keys_and_service(options, head)[0]
|
213
|
+
end
|
214
|
+
|
215
|
+
# Same as +keys+ method but return an array of [keys, service_data].
|
216
|
+
# where +service_data+ is a hash with additional output information.
|
217
|
+
#
|
218
|
+
# keys, service = bucket.keys_and_service({'max-keys'=> 2, 'prefix' => 'logs'})
|
219
|
+
# p keys #=> # 2 keys array
|
220
|
+
# p service #=> {"max-keys"=>"2", "prefix"=>"logs", "name"=>"my_awesome_bucket", "marker"=>"", "is_truncated"=>true}
|
221
|
+
#
|
222
|
+
def keys_and_service(options={}, head=false)
|
223
|
+
opt = {}; options.each{ |key, value| opt[key.to_s] = value }
|
224
|
+
service_data = {}
|
225
|
+
thislist = {}
|
226
|
+
list = []
|
227
|
+
@s3.interface.incrementally_list_bucket(@name, opt) do |thislist|
|
228
|
+
thislist[:contents].each do |entry|
|
229
|
+
owner = Owner.new(entry[:owner_id], entry[:owner_display_name])
|
230
|
+
key = Key.new(self, entry[:key], nil, {}, {}, entry[:last_modified], entry[:e_tag], entry[:size], entry[:storage_class], owner)
|
231
|
+
key.head if head
|
232
|
+
list << key
|
233
|
+
end
|
234
|
+
end
|
235
|
+
thislist.each_key do |key|
|
236
|
+
service_data[key] = thislist[key] unless (key == :contents || key == :common_prefixes)
|
237
|
+
end
|
238
|
+
[list, service_data]
|
239
|
+
end
|
240
|
+
|
241
|
+
# Retrieve key information from Amazon.
|
242
|
+
# The +key_name+ is a +String+ or Key instance.
|
243
|
+
# Retrieves meta-header information if +head+ is +true+.
|
244
|
+
# Returns new Key instance.
|
245
|
+
#
|
246
|
+
# key = bucket.key('logs/today/1.log', true) #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
247
|
+
# # is the same as:
|
248
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
|
249
|
+
# key.head
|
250
|
+
#
|
251
|
+
def key(key_name, head=false)
|
252
|
+
raise 'Key name can not be empty.' if key_name.blank?
|
253
|
+
key_instance = nil
|
254
|
+
# if this key exists - find it ....
|
255
|
+
keys({'prefix'=>key_name}, head).each do |key|
|
256
|
+
if key.name == key_name.to_s
|
257
|
+
key_instance = key
|
258
|
+
break
|
259
|
+
end
|
260
|
+
end
|
261
|
+
# .... else this key is unknown
|
262
|
+
unless key_instance
|
263
|
+
key_instance = Key.create(self, key_name.to_s)
|
264
|
+
end
|
265
|
+
key_instance
|
266
|
+
end
|
267
|
+
|
268
|
+
# Store object data.
|
269
|
+
# The +key+ is a +String+ or Key instance.
|
270
|
+
# Returns +true+.
|
271
|
+
#
|
272
|
+
# bucket.put('logs/today/1.log', 'Olala!') #=> true
|
273
|
+
#
|
274
|
+
def put(key, data=nil, meta_headers={}, perms=nil, headers={})
|
275
|
+
key = Key.create(self, key.to_s, data, meta_headers) unless key.is_a?(Key)
|
276
|
+
key.put(data, perms, headers)
|
277
|
+
end
|
278
|
+
|
279
|
+
# Retrieve object data from Amazon.
|
280
|
+
# The +key+ is a +String+ or Key.
|
281
|
+
# Returns Key instance.
|
282
|
+
#
|
283
|
+
# key = bucket.get('logs/today/1.log') #=>
|
284
|
+
# puts key.data #=> 'sasfasfasdf'
|
285
|
+
#
|
286
|
+
def get(key, headers={})
|
287
|
+
key = Key.create(self, key.to_s) unless key.is_a?(Key)
|
288
|
+
key.get(headers)
|
289
|
+
end
|
290
|
+
|
291
|
+
# Rename object. Returns RightAws::S3::Key instance.
|
292
|
+
#
|
293
|
+
# new_key = bucket.rename_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
294
|
+
# puts key.name #=> 'logs/today/2.log'
|
295
|
+
# key.exists? #=> true
|
296
|
+
#
|
297
|
+
def rename_key(old_key_or_name, new_name)
|
298
|
+
old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key)
|
299
|
+
old_key_or_name.rename(new_name)
|
300
|
+
old_key_or_name
|
301
|
+
end
|
302
|
+
|
303
|
+
# Create an object copy. Returns a destination RightAws::S3::Key instance.
|
304
|
+
#
|
305
|
+
# new_key = bucket.copy_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
306
|
+
# puts key.name #=> 'logs/today/2.log'
|
307
|
+
# key.exists? #=> true
|
308
|
+
#
|
309
|
+
def copy_key(old_key_or_name, new_key_or_name)
|
310
|
+
old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key)
|
311
|
+
old_key_or_name.copy(new_key_or_name)
|
312
|
+
end
|
313
|
+
|
314
|
+
# Move an object to other location. Returns a destination RightAws::S3::Key instance.
|
315
|
+
#
|
316
|
+
# new_key = bucket.copy_key('logs/today/1.log','logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
317
|
+
# puts key.name #=> 'logs/today/2.log'
|
318
|
+
# key.exists? #=> true
|
319
|
+
#
|
320
|
+
def move_key(old_key_or_name, new_key_or_name)
|
321
|
+
old_key_or_name = Key.create(self, old_key_or_name.to_s) unless old_key_or_name.is_a?(Key)
|
322
|
+
old_key_or_name.move(new_key_or_name)
|
323
|
+
end
|
324
|
+
|
325
|
+
# Remove all keys from a bucket.
|
326
|
+
# Returns +true+.
|
327
|
+
#
|
328
|
+
# bucket.clear #=> true
|
329
|
+
#
|
330
|
+
def clear
|
331
|
+
@s3.interface.clear_bucket(@name)
|
332
|
+
end
|
333
|
+
|
334
|
+
# Delete all keys where the 'folder_key' can be interpreted
|
335
|
+
# as a 'folder' name.
|
336
|
+
# Returns an array of string keys that have been deleted.
|
337
|
+
#
|
338
|
+
# bucket.keys.map{|key| key.name}.join(', ') #=> 'test, test/2/34, test/3, test1, test1/logs'
|
339
|
+
# bucket.delete_folder('test') #=> ['test','test/2/34','test/3']
|
340
|
+
#
|
341
|
+
def delete_folder(folder, separator='/')
|
342
|
+
@s3.interface.delete_folder(@name, folder, separator)
|
343
|
+
end
|
344
|
+
|
345
|
+
# Delete a bucket. Bucket must be empty.
|
346
|
+
# If +force+ is set, clears and deletes the bucket.
|
347
|
+
# Returns +true+.
|
348
|
+
#
|
349
|
+
# bucket.delete(true) #=> true
|
350
|
+
#
|
351
|
+
def delete(force=false)
|
352
|
+
force ? @s3.interface.force_delete_bucket(@name) : @s3.interface.delete_bucket(@name)
|
353
|
+
end
|
354
|
+
|
355
|
+
# Return a list of grantees.
|
356
|
+
#
|
357
|
+
def grantees
|
358
|
+
Grantee::grantees(self)
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
|
364
|
+
class Key
|
365
|
+
attr_reader :bucket, :name, :last_modified, :e_tag, :size, :storage_class, :owner
|
366
|
+
attr_accessor :headers, :meta_headers
|
367
|
+
attr_writer :data
|
368
|
+
|
369
|
+
# Separate Amazon meta headers from other headers
|
370
|
+
def self.split_meta(headers) #:nodoc:
|
371
|
+
hash = headers.dup
|
372
|
+
meta = {}
|
373
|
+
hash.each do |key, value|
|
374
|
+
if key[/^#{S3Interface::AMAZON_METADATA_PREFIX}/]
|
375
|
+
meta[key.gsub(S3Interface::AMAZON_METADATA_PREFIX,'')] = value
|
376
|
+
hash.delete(key)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
[hash, meta]
|
380
|
+
end
|
381
|
+
|
382
|
+
def self.add_meta_prefix(meta_headers, prefix=S3Interface::AMAZON_METADATA_PREFIX)
|
383
|
+
meta = {}
|
384
|
+
meta_headers.each do |meta_header, value|
|
385
|
+
if meta_header[/#{prefix}/]
|
386
|
+
meta[meta_header] = value
|
387
|
+
else
|
388
|
+
meta["#{S3Interface::AMAZON_METADATA_PREFIX}#{meta_header}"] = value
|
389
|
+
end
|
390
|
+
end
|
391
|
+
meta
|
392
|
+
end
|
393
|
+
|
394
|
+
|
395
|
+
# Create a new Key instance, but do not create the actual key.
|
396
|
+
# The +name+ is a +String+.
|
397
|
+
# Returns a new Key instance.
|
398
|
+
#
|
399
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
400
|
+
# key.exists? #=> true | false
|
401
|
+
# key.put('Woohoo!') #=> true
|
402
|
+
# key.exists? #=> true
|
403
|
+
#
|
404
|
+
def self.create(bucket, name, data=nil, meta_headers={})
|
405
|
+
new(bucket, name, data, {}, meta_headers)
|
406
|
+
end
|
407
|
+
|
408
|
+
# Create a new Key instance, but do not create the actual key.
|
409
|
+
# In normal use this method should not be called directly.
|
410
|
+
# Use RightAws::S3::Key.create or bucket.key() instead.
|
411
|
+
#
|
412
|
+
def initialize(bucket, name, data=nil, headers={}, meta_headers={},
|
413
|
+
last_modified=nil, e_tag=nil, size=nil, storage_class=nil, owner=nil)
|
414
|
+
raise 'Bucket must be a Bucket instance.' unless bucket.is_a?(Bucket)
|
415
|
+
@bucket = bucket
|
416
|
+
@name = name
|
417
|
+
@data = data
|
418
|
+
@e_tag = e_tag
|
419
|
+
@size = size.to_i
|
420
|
+
@storage_class = storage_class
|
421
|
+
@owner = owner
|
422
|
+
@last_modified = last_modified
|
423
|
+
if @last_modified && !@last_modified.is_a?(Time)
|
424
|
+
@last_modified = Time.parse(@last_modified)
|
425
|
+
end
|
426
|
+
@headers, @meta_headers = self.class.split_meta(headers)
|
427
|
+
@meta_headers.merge!(meta_headers)
|
428
|
+
end
|
429
|
+
|
430
|
+
# Return key name as a String.
|
431
|
+
#
|
432
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
433
|
+
# puts key #=> 'logs/today/1.log'
|
434
|
+
#
|
435
|
+
def to_s
|
436
|
+
@name.to_s
|
437
|
+
end
|
438
|
+
|
439
|
+
# Return the full S3 path to this key (bucket/key).
|
440
|
+
#
|
441
|
+
# key.full_name #=> 'my_awesome_bucket/cool_key'
|
442
|
+
#
|
443
|
+
def full_name(separator='/')
|
444
|
+
"#{@bucket.to_s}#{separator}#{@name}"
|
445
|
+
end
|
446
|
+
|
447
|
+
# Return a public link to a key.
|
448
|
+
#
|
449
|
+
# key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'
|
450
|
+
#
|
451
|
+
def public_link
|
452
|
+
params = @bucket.s3.interface.params
|
453
|
+
"#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name('/')}"
|
454
|
+
end
|
455
|
+
|
456
|
+
# Return Key data. Retrieve this data from Amazon if it is the first time call.
|
457
|
+
# TODO TRB 6/19/07 What does the above mean? Clarify.
|
458
|
+
#
|
459
|
+
def data
|
460
|
+
get if !@data and exists?
|
461
|
+
@data
|
462
|
+
end
|
463
|
+
|
464
|
+
# Retrieve object data and attributes from Amazon.
|
465
|
+
# Returns a +String+.
|
466
|
+
#
|
467
|
+
def get(headers={})
|
468
|
+
response = @bucket.s3.interface.get(@bucket.name, @name, headers)
|
469
|
+
@data = response[:object]
|
470
|
+
@headers, @meta_headers = self.class.split_meta(response[:headers])
|
471
|
+
refresh(false)
|
472
|
+
@data
|
473
|
+
end
|
474
|
+
|
475
|
+
# Store object data on S3.
|
476
|
+
# Parameter +data+ is a +String+ or S3Object instance.
|
477
|
+
# Returns +true+.
|
478
|
+
#
|
479
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
|
480
|
+
# key.data = 'Qwerty'
|
481
|
+
# key.put #=> true
|
482
|
+
# ...
|
483
|
+
# key.put('Olala!') #=> true
|
484
|
+
#
|
485
|
+
def put(data=nil, perms=nil, headers={})
|
486
|
+
headers['x-amz-acl'] = perms if perms
|
487
|
+
@data = data || @data
|
488
|
+
meta = self.class.add_meta_prefix(@meta_headers)
|
489
|
+
@bucket.s3.interface.put(@bucket.name, @name, @data, meta.merge(headers))
|
490
|
+
end
|
491
|
+
|
492
|
+
# Rename an object. Returns new object name.
|
493
|
+
#
|
494
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
495
|
+
# key.rename('logs/today/2.log') #=> 'logs/today/2.log'
|
496
|
+
# puts key.name #=> 'logs/today/2.log'
|
497
|
+
# key.exists? #=> true
|
498
|
+
#
|
499
|
+
def rename(new_name)
|
500
|
+
@bucket.s3.interface.rename(@bucket.name, @name, new_name)
|
501
|
+
@name = new_name
|
502
|
+
end
|
503
|
+
|
504
|
+
# Create an object copy. Returns a destination RightAws::S3::Key instance.
|
505
|
+
#
|
506
|
+
# # Key instance as destination
|
507
|
+
# key1 = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
508
|
+
# key2 = RightAws::S3::Key.create(bucket, 'logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
|
509
|
+
# key1.put('Olala!') #=> true
|
510
|
+
# key1.copy(key2) #=> #<RightAws::S3::Key:0xb7b5e240 ... >
|
511
|
+
# key1.exists? #=> true
|
512
|
+
# key2.exists? #=> true
|
513
|
+
# puts key2.data #=> 'Olala!'
|
514
|
+
#
|
515
|
+
# # String as destination
|
516
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/777.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
517
|
+
# key.put('Olala!') #=> true
|
518
|
+
# new_key = key.copy('logs/today/888.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
|
519
|
+
# key.exists? #=> true
|
520
|
+
# new_key.exists? #=> true
|
521
|
+
#
|
522
|
+
def copy(new_key_or_name)
|
523
|
+
new_key_or_name = Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(Key)
|
524
|
+
@bucket.s3.interface.copy(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name)
|
525
|
+
new_key_or_name
|
526
|
+
end
|
527
|
+
|
528
|
+
# Move an object to other location. Returns a destination RightAws::S3::Key instance.
|
529
|
+
#
|
530
|
+
# # Key instance as destination
|
531
|
+
# key1 = RightAws::S3::Key.create(bucket, 'logs/today/1.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
532
|
+
# key2 = RightAws::S3::Key.create(bucket, 'logs/today/2.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
|
533
|
+
# key1.put('Olala!') #=> true
|
534
|
+
# key1.move(key2) #=> #<RightAws::S3::Key:0xb7b5e240 ... >
|
535
|
+
# key1.exists? #=> false
|
536
|
+
# key2.exists? #=> true
|
537
|
+
# puts key2.data #=> 'Olala!'
|
538
|
+
#
|
539
|
+
# # String as destination
|
540
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/777.log') #=> #<RightAws::S3::Key:0xb7b1e240 ... >
|
541
|
+
# key.put('Olala!') #=> true
|
542
|
+
# new_key = key.move('logs/today/888.log') #=> #<RightAws::S3::Key:0xb7b5e240 ... >
|
543
|
+
# key.exists? #=> false
|
544
|
+
# new_key.exists? #=> true
|
545
|
+
#
|
546
|
+
def move(new_key_or_name)
|
547
|
+
new_key_or_name = Key.create(@bucket, new_key_or_name.to_s) unless new_key_or_name.is_a?(Key)
|
548
|
+
@bucket.s3.interface.move(@bucket.name, @name, new_key_or_name.bucket.name, new_key_or_name.name)
|
549
|
+
new_key_or_name
|
550
|
+
end
|
551
|
+
|
552
|
+
# Retrieve key info from bucket and update attributes.
|
553
|
+
# Refresh meta-headers (by calling +head+ method) if +head+ is set.
|
554
|
+
# Returns +true+ if the key exists in bucket and +false+ otherwise.
|
555
|
+
#
|
556
|
+
# key = RightAws::S3::Key.create(bucket, 'logs/today/1.log')
|
557
|
+
# key.e_tag #=> nil
|
558
|
+
# key.meta_headers #=> {}
|
559
|
+
# key.refresh #=> true
|
560
|
+
# key.e_tag #=> '12345678901234567890bf11094484b6'
|
561
|
+
# key.meta_headers #=> {"family"=>"qwerty", "name"=>"asdfg"}
|
562
|
+
#
|
563
|
+
def refresh(head=true)
|
564
|
+
new_key = @bucket.key(self)
|
565
|
+
@last_modified = new_key.last_modified
|
566
|
+
@e_tag = new_key.e_tag
|
567
|
+
@size = new_key.size
|
568
|
+
@storage_class = new_key.storage_class
|
569
|
+
@owner = new_key.owner
|
570
|
+
if @last_modified
|
571
|
+
self.head
|
572
|
+
true
|
573
|
+
else
|
574
|
+
@headers = @meta_headers = {}
|
575
|
+
false
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
579
|
+
# Updates headers and meta-headers from S3.
|
580
|
+
# Returns +true+.
|
581
|
+
#
|
582
|
+
# key.meta_headers #=> {"family"=>"qwerty"}
|
583
|
+
# key.head #=> true
|
584
|
+
# key.meta_headers #=> {"family"=>"qwerty", "name"=>"asdfg"}
|
585
|
+
#
|
586
|
+
def head
|
587
|
+
@headers, @meta_headers = self.class.split_meta(@bucket.s3.interface.head(@bucket, @name))
|
588
|
+
true
|
589
|
+
end
|
590
|
+
|
591
|
+
# Reload meta-headers only. Returns meta-headers hash.
|
592
|
+
#
|
593
|
+
# key.reload_meta #=> {"family"=>"qwerty", "name"=>"asdfg"}
|
594
|
+
#
|
595
|
+
def reload_meta
|
596
|
+
@meta_headers = self.class.split_meta(@bucket.s3.interface.head(@bucket, @name)).last
|
597
|
+
end
|
598
|
+
|
599
|
+
# Replace meta-headers by new hash at S3. Returns new meta-headers hash.
|
600
|
+
#
|
601
|
+
# key.reload_meta #=> {"family"=>"qwerty", "name"=>"asdfg"}
|
602
|
+
# key.save_meta #=> {"family"=>"oops", "race" => "troll"}
|
603
|
+
# key.reload_meta #=> {"family"=>"oops", "race" => "troll"}
|
604
|
+
#
|
605
|
+
def save_meta(meta_headers)
|
606
|
+
meta = self.class.add_meta_prefix(meta_headers)
|
607
|
+
@bucket.s3.interface.copy(@bucket.name, @name, @bucket.name, @name, :replace, meta)
|
608
|
+
@meta_headers = self.class.split_meta(meta)[1]
|
609
|
+
end
|
610
|
+
|
611
|
+
# Check for existence of the key in the given bucket.
|
612
|
+
# Returns +true+ or +false+.
|
613
|
+
#
|
614
|
+
# key = RightAws::S3::Key.create(bucket,'logs/today/1.log')
|
615
|
+
# key.exists? #=> false
|
616
|
+
# key.put('Woohoo!') #=> true
|
617
|
+
# key.exists? #=> true
|
618
|
+
#
|
619
|
+
def exists?
|
620
|
+
@bucket.key(self).last_modified ? true : false
|
621
|
+
end
|
622
|
+
|
623
|
+
# Remove key from bucket.
|
624
|
+
# Returns +true+.
|
625
|
+
#
|
626
|
+
# key.delete #=> true
|
627
|
+
#
|
628
|
+
def delete
|
629
|
+
raise 'Key name must be specified.' if @name.blank?
|
630
|
+
@bucket.s3.interface.delete(@bucket, @name)
|
631
|
+
end
|
632
|
+
|
633
|
+
# Return a list of grantees.
|
634
|
+
#
|
635
|
+
def grantees
|
636
|
+
Grantee::grantees(self)
|
637
|
+
end
|
638
|
+
|
639
|
+
end
|
640
|
+
|
641
|
+
|
642
|
+
class Owner
|
643
|
+
attr_reader :id, :name
|
644
|
+
|
645
|
+
def initialize(id, name)
|
646
|
+
@id = id
|
647
|
+
@name = name
|
648
|
+
end
|
649
|
+
|
650
|
+
# Return Owner name as a +String+.
|
651
|
+
def to_s
|
652
|
+
@name
|
653
|
+
end
|
654
|
+
end
|
655
|
+
|
656
|
+
|
657
|
+
# There are 2 ways to set permissions for a bucket or key (called a +thing+ below):
|
658
|
+
#
|
659
|
+
# 1 . Use +perms+ param to set 'Canned Access Policies' when calling the <tt>bucket.create</tt>,
|
660
|
+
# <tt>bucket.put</tt> and <tt>key.put</tt> methods.
|
661
|
+
# The +perms+ param can take these values: 'private', 'public-read', 'public-read-write' and
|
662
|
+
# 'authenticated-read'.
|
663
|
+
# (see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html).
|
664
|
+
#
|
665
|
+
# bucket = s3.bucket('bucket_for_kd_test_13', true, 'public-read')
|
666
|
+
# key.put('Woohoo!','public-read-write' )
|
667
|
+
#
|
668
|
+
# 2 . Use Grantee instances (the permission is a +String+ or an +Array+ of: 'READ', 'WRITE',
|
669
|
+
# 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL'):
|
670
|
+
#
|
671
|
+
# bucket = s3.bucket('my_awesome_bucket', true)
|
672
|
+
# grantee1 = RightAws::S3::Grantee.new(bucket, 'a123b...223c', FULL_CONTROL, :apply)
|
673
|
+
# grantee2 = RightAws::S3::Grantee.new(bucket, 'xy3v3...5fhp', [READ, WRITE], :apply)
|
674
|
+
#
|
675
|
+
# There is only one way to get and to remove permission (via Grantee instances):
|
676
|
+
#
|
677
|
+
# grantees = bucket.grantees # a list of Grantees that have any access for this bucket
|
678
|
+
# grantee1 = RightAws::S3::Grantee.new(bucket, 'a123b...223c')
|
679
|
+
# grantee1.perms #=> returns a list of perms for this grantee to that bucket
|
680
|
+
# ...
|
681
|
+
# grantee1.drop # remove all perms for this grantee
|
682
|
+
# grantee2.revoke('WRITE') # revoke write access only
|
683
|
+
#
|
684
|
+
class Grantee
|
685
|
+
# A bucket or a key the grantee has an access to.
|
686
|
+
attr_reader :thing
|
687
|
+
# Grantee Amazon id.
|
688
|
+
attr_reader :id
|
689
|
+
# Grantee display name.
|
690
|
+
attr_reader :name
|
691
|
+
# Array of permissions.
|
692
|
+
attr_accessor :perms
|
693
|
+
|
694
|
+
# Retrieve Owner information and a list of Grantee instances that have
|
695
|
+
# a access to this thing (bucket or key).
|
696
|
+
#
|
697
|
+
# bucket = s3.bucket('my_awesome_bucket', true, 'public-read')
|
698
|
+
# ...
|
699
|
+
# RightAws::S3::Grantee.owner_and_grantees(bucket) #=> [owner, grantees]
|
700
|
+
#
|
701
|
+
def self.owner_and_grantees(thing)
|
702
|
+
if thing.is_a?(Bucket)
|
703
|
+
bucket, key = thing, ''
|
704
|
+
else
|
705
|
+
bucket, key = thing.bucket, thing
|
706
|
+
end
|
707
|
+
hash = bucket.s3.interface.get_acl_parse(bucket.to_s, key.to_s)
|
708
|
+
owner = Owner.new(hash[:owner][:id], hash[:owner][:display_name])
|
709
|
+
|
710
|
+
grantees = []
|
711
|
+
hash[:grantees].each do |id, params|
|
712
|
+
grantees << new(thing, id, params[:permissions], nil, params[:display_name])
|
713
|
+
end
|
714
|
+
[owner, grantees]
|
715
|
+
end
|
716
|
+
|
717
|
+
# Retrieves a list of Grantees instances that have an access to this thing(bucket or key).
|
718
|
+
#
|
719
|
+
# bucket = s3.bucket('my_awesome_bucket', true, 'public-read')
|
720
|
+
# ...
|
721
|
+
# RightAws::S3::Grantee.grantees(bucket) #=> grantees
|
722
|
+
#
|
723
|
+
def self.grantees(thing)
|
724
|
+
owner_and_grantees(thing)[1]
|
725
|
+
end
|
726
|
+
|
727
|
+
def self.put_acl(thing, owner, grantees) #:nodoc:
|
728
|
+
if thing.is_a?(Bucket)
|
729
|
+
bucket, key = thing, ''
|
730
|
+
else
|
731
|
+
bucket, key = thing.bucket, thing
|
732
|
+
end
|
733
|
+
body = "<AccessControlPolicy>" +
|
734
|
+
"<Owner>" +
|
735
|
+
"<ID>#{owner.id}</ID>" +
|
736
|
+
"<DisplayName>#{owner.name}</DisplayName>" +
|
737
|
+
"</Owner>" +
|
738
|
+
"<AccessControlList>" +
|
739
|
+
grantees.map{|grantee| grantee.to_xml}.join +
|
740
|
+
"</AccessControlList>" +
|
741
|
+
"</AccessControlPolicy>"
|
742
|
+
bucket.s3.interface.put_acl(bucket.to_s, key.to_s, body)
|
743
|
+
end
|
744
|
+
|
745
|
+
# Create a new Grantee instance.
|
746
|
+
# Grantee +id+ must exist on S3. If +action+ == :refresh, then retrieve
|
747
|
+
# permissions from S3 and update @perms. If +action+ == :apply, then apply
|
748
|
+
# perms to +thing+ at S3. If +action+ == :apply_and_refresh then it performs.
|
749
|
+
# both the actions. This is used for the new grantees that had no perms to
|
750
|
+
# this thing before. The default action is :refresh.
|
751
|
+
#
|
752
|
+
# bucket = s3.bucket('my_awesome_bucket', true, 'public-read')
|
753
|
+
# grantee1 = RightAws::S3::Grantee.new(bucket, 'a123b...223c', FULL_CONTROL)
|
754
|
+
# ...
|
755
|
+
# grantee2 = RightAws::S3::Grantee.new(bucket, 'abcde...asdf', [FULL_CONTROL, READ], :apply)
|
756
|
+
# grantee3 = RightAws::S3::Grantee.new(bucket, 'aaaaa...aaaa', 'READ', :apply_and_refresh)
|
757
|
+
#
|
758
|
+
def initialize(thing, id, perms=[], action=:refresh, name=nil)
|
759
|
+
@thing = thing
|
760
|
+
@id = id
|
761
|
+
@name = name
|
762
|
+
@perms = perms.to_a
|
763
|
+
case action
|
764
|
+
when :apply: apply
|
765
|
+
when :refresh: refresh
|
766
|
+
when :apply_and_refresh: apply; refresh
|
767
|
+
end
|
768
|
+
end
|
769
|
+
|
770
|
+
# Return +true+ if the grantee has any permissions to the thing.
|
771
|
+
def exists?
|
772
|
+
self.class.grantees(@thing).each do |grantee|
|
773
|
+
return true if @id == grantee.id
|
774
|
+
end
|
775
|
+
false
|
776
|
+
end
|
777
|
+
|
778
|
+
# Return Grantee type (+String+): "Group" or "CanonicalUser".
|
779
|
+
def type
|
780
|
+
@id[/^http:/] ? "Group" : "CanonicalUser"
|
781
|
+
end
|
782
|
+
|
783
|
+
# Return a name or an id.
|
784
|
+
def to_s
|
785
|
+
@name || @id
|
786
|
+
end
|
787
|
+
|
788
|
+
# Add permissions for grantee.
|
789
|
+
# Permissions: 'READ', 'WRITE', 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL'.
|
790
|
+
# See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingPermissions.html .
|
791
|
+
# Returns +true+.
|
792
|
+
#
|
793
|
+
# grantee.grant('FULL_CONTROL') #=> true
|
794
|
+
# grantee.grant('FULL_CONTROL','WRITE','READ') #=> true
|
795
|
+
# grantee.grant(['WRITE_ACP','READ','READ_ACP']) #=> true
|
796
|
+
#
|
797
|
+
def grant(*permissions)
|
798
|
+
permissions.flatten!
|
799
|
+
old_perms = @perms.dup
|
800
|
+
@perms += permissions
|
801
|
+
@perms.uniq!
|
802
|
+
return true if @perms == old_perms
|
803
|
+
apply
|
804
|
+
end
|
805
|
+
|
806
|
+
# Revoke permissions for grantee.
|
807
|
+
# Permissions: 'READ', 'WRITE', 'READ_ACP', 'WRITE_ACP', 'FULL_CONTROL'
|
808
|
+
# See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingPermissions.html .
|
809
|
+
# Default value is 'FULL_CONTROL'.
|
810
|
+
# Returns +true+.
|
811
|
+
#
|
812
|
+
# grantee.revoke('READ') #=> true
|
813
|
+
# grantee.revoke('FULL_CONTROL','WRITE') #=> true
|
814
|
+
# grantee.revoke(['READ_ACP','WRITE_ACP']) #=> true
|
815
|
+
#
|
816
|
+
def revoke(*permissions)
|
817
|
+
permissions.flatten!
|
818
|
+
old_perms = @perms.dup
|
819
|
+
@perms -= permissions
|
820
|
+
@perms.uniq!
|
821
|
+
return true if @perms == old_perms
|
822
|
+
apply
|
823
|
+
end
|
824
|
+
|
825
|
+
# Revoke all permissions for this grantee.
|
826
|
+
# Returns +true+.
|
827
|
+
#
|
828
|
+
# grantee.drop #=> true
|
829
|
+
#
|
830
|
+
def drop
|
831
|
+
@perms = []
|
832
|
+
apply
|
833
|
+
end
|
834
|
+
|
835
|
+
# Refresh grantee perms for its +thing+.
|
836
|
+
# Returns +true+ if the grantee has perms for this +thing+ or
|
837
|
+
# +false+ otherwise, and updates @perms value as a side-effect.
|
838
|
+
#
|
839
|
+
# grantee.grant('FULL_CONTROL') #=> true
|
840
|
+
# grantee.refresh #=> true
|
841
|
+
# grantee.drop #=> true
|
842
|
+
# grantee.refresh #=> false
|
843
|
+
#
|
844
|
+
def refresh
|
845
|
+
@perms = []
|
846
|
+
self.class.grantees(@thing).each do |grantee|
|
847
|
+
if @id == grantee.id
|
848
|
+
@name = grantee.name
|
849
|
+
@perms = grantee.perms
|
850
|
+
return true
|
851
|
+
end
|
852
|
+
end
|
853
|
+
false
|
854
|
+
end
|
855
|
+
|
856
|
+
# Apply current grantee @perms to +thing+. This method is called internally by the +grant+
|
857
|
+
# and +revoke+ methods. In normal use this method should not
|
858
|
+
# be called directly.
|
859
|
+
#
|
860
|
+
# grantee.perms = ['FULL_CONTROL']
|
861
|
+
# grantee.apply #=> true
|
862
|
+
#
|
863
|
+
def apply
|
864
|
+
@perms.uniq!
|
865
|
+
owner, grantees = self.class.owner_and_grantees(@thing)
|
866
|
+
# walk through all the grantees and replace the data for the current one and ...
|
867
|
+
grantees.map! { |grantee| grantee.id == @id ? self : grantee }
|
868
|
+
# ... if this grantee is not known - add this bad boy to a list
|
869
|
+
grantees << self unless grantees.include?(self)
|
870
|
+
# set permissions
|
871
|
+
self.class.put_acl(@thing, owner, grantees)
|
872
|
+
end
|
873
|
+
|
874
|
+
def to_xml # :nodoc:
|
875
|
+
id_str = @id[/^http/] ? "<URI>#{@id}</URI>" : "<ID>#{@id}</ID>"
|
876
|
+
grants = ''
|
877
|
+
@perms.each do |perm|
|
878
|
+
grants << "<Grant>" +
|
879
|
+
"<Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
|
880
|
+
"xsi:type=\"#{type}\">#{id_str}</Grantee>" +
|
881
|
+
"<Permission>#{perm}</Permission>" +
|
882
|
+
"</Grant>"
|
883
|
+
end
|
884
|
+
grants
|
885
|
+
end
|
886
|
+
|
887
|
+
end
|
888
|
+
|
889
|
+
end
|
890
|
+
|
891
|
+
# RightAws::S3Generator and RightAws::S3Generator::Bucket methods:
|
892
|
+
#
|
893
|
+
# s3g = RightAws::S3Generator.new('1...2', 'nx...Y6') #=> #<RightAws::S3Generator:0xb7b5cc94>
|
894
|
+
#
|
895
|
+
# # List all buckets(method 'GET'):
|
896
|
+
# buckets_list = s3g.buckets #=> 'https://s3.amazonaws.com:443/?Signature=Y...D&Expires=1180941864&AWSAccessKeyId=1...2'
|
897
|
+
# # Create bucket link (method 'PUT'):
|
898
|
+
# bucket = s3g.bucket('my_awesome_bucket') #=> #<RightAws::S3Generator::Bucket:0xb7bcbda8>
|
899
|
+
# link_to_create = bucket.create_link(1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=4...D&Expires=1180942132&AWSAccessKeyId=1...2
|
900
|
+
# # ... or:
|
901
|
+
# bucket = RightAws::S3Generator::Bucket.create(s3g, 'my_awesome_bucket') #=> #<RightAws::S3Generator::Bucket:0xb7bcbda8>
|
902
|
+
# link_to_create = bucket.create_link(1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=4...D&Expires=1180942132&AWSAccessKeyId=1...2
|
903
|
+
# # ... or:
|
904
|
+
# bucket = RightAws::S3Generator::Bucket.new(s3g, 'my_awesome_bucket') #=> #<RightAws::S3Generator::Bucket:0xb7bcbda8>
|
905
|
+
# link_to_create = bucket.create_link(1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=4...D&Expires=1180942132&AWSAccessKeyId=1...2
|
906
|
+
# # List bucket(method 'GET'):
|
907
|
+
# bucket.keys(1.day) #=> https://s3.amazonaws.com:443/my_awesome_bucket?Signature=i...D&Expires=1180942620&AWSAccessKeyId=1...2
|
908
|
+
# # Create/put key (method 'PUT'):
|
909
|
+
# bucket.put('my_cool_key') #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=q...D&Expires=1180943094&AWSAccessKeyId=1...2
|
910
|
+
# # Get key data (method 'GET'):
|
911
|
+
# bucket.get('logs/today/1.log', 1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=h...M%3D&Expires=1180820032&AWSAccessKeyId=1...2
|
912
|
+
# # Delete bucket (method 'DELETE'):
|
913
|
+
# bucket.delete(2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=4...D&Expires=1180820032&AWSAccessKeyId=1...2
|
914
|
+
#
|
915
|
+
# RightAws::S3Generator::Key methods:
|
916
|
+
#
|
917
|
+
# # Create Key instance:
|
918
|
+
# key = RightAws::S3Generator::Key.new(bicket, 'my_cool_key') #=> #<RightAws::S3Generator::Key:0xb7b7394c>
|
919
|
+
# # Put key data (method 'PUT'):
|
920
|
+
# key.put #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=2...D&Expires=1180943302&AWSAccessKeyId=1...2
|
921
|
+
# # Get key data (method 'GET'):
|
922
|
+
# key.get #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=a...D&Expires=1180820032&AWSAccessKeyId=1...2
|
923
|
+
# # Head key (method 'HEAD'):
|
924
|
+
# key.head #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=b...D&Expires=1180820032&AWSAccessKeyId=1...2
|
925
|
+
# # Delete key (method 'DELETE'):
|
926
|
+
# key.delete #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=x...D&Expires=1180820032&AWSAccessKeyId=1...2
|
927
|
+
#
|
928
|
+
class S3Generator
|
929
|
+
attr_reader :interface
|
930
|
+
|
931
|
+
def initialize(aws_access_key_id, aws_secret_access_key, params={})
|
932
|
+
@interface = S3Interface.new(aws_access_key_id, aws_secret_access_key, params)
|
933
|
+
end
|
934
|
+
|
935
|
+
# Generate link to list all buckets
|
936
|
+
#
|
937
|
+
# s3.buckets(1.hour)
|
938
|
+
#
|
939
|
+
def buckets(expires=nil, headers={})
|
940
|
+
@interface.list_all_my_buckets_link(expires, headers)
|
941
|
+
end
|
942
|
+
|
943
|
+
# Create new S3LinkBucket instance and generate link to create it at S3.
|
944
|
+
#
|
945
|
+
# bucket= s3.bucket('my_owesome_bucket')
|
946
|
+
#
|
947
|
+
def bucket(name, expires=nil, headers={})
|
948
|
+
Bucket.create(self, name.to_s)
|
949
|
+
end
|
950
|
+
|
951
|
+
class Bucket
|
952
|
+
attr_reader :s3, :name
|
953
|
+
|
954
|
+
def to_s
|
955
|
+
@name
|
956
|
+
end
|
957
|
+
alias_method :full_name, :to_s
|
958
|
+
|
959
|
+
# Return a public link to bucket.
|
960
|
+
#
|
961
|
+
# bucket.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket'
|
962
|
+
#
|
963
|
+
def public_link
|
964
|
+
params = @s3.interface.params
|
965
|
+
"#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name}"
|
966
|
+
end
|
967
|
+
|
968
|
+
# Create new S3LinkBucket instance and generate creation link for it.
|
969
|
+
def self.create(s3, name, expires=nil, headers={})
|
970
|
+
new(s3, name.to_s)
|
971
|
+
end
|
972
|
+
|
973
|
+
# Create new S3LinkBucket instance.
|
974
|
+
def initialize(s3, name)
|
975
|
+
@s3, @name = s3, name.to_s
|
976
|
+
end
|
977
|
+
|
978
|
+
# Return a link to create this bucket.
|
979
|
+
#
|
980
|
+
def create_link(expires=nil, headers={})
|
981
|
+
@s3.interface.create_bucket_link(@name, expires, headers)
|
982
|
+
end
|
983
|
+
|
984
|
+
# Generate link to list keys.
|
985
|
+
#
|
986
|
+
# bucket.keys
|
987
|
+
# bucket.keys('prefix'=>'logs')
|
988
|
+
#
|
989
|
+
def keys(options=nil, expires=nil, headers={})
|
990
|
+
@s3.interface.list_bucket_link(@name, options, expires, headers)
|
991
|
+
end
|
992
|
+
|
993
|
+
# Return a S3Generator::Key instance.
|
994
|
+
#
|
995
|
+
# bucket.key('my_cool_key').get #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=B...D&Expires=1180820032&AWSAccessKeyId=1...2
|
996
|
+
# bucket.key('my_cool_key').delete #=> https://s3.amazonaws.com:443/my_awesome_bucket/my_cool_key?Signature=B...D&Expires=1180820098&AWSAccessKeyId=1...2
|
997
|
+
#
|
998
|
+
def key(name)
|
999
|
+
Key.new(self, name)
|
1000
|
+
end
|
1001
|
+
|
1002
|
+
# Generates link to PUT key data.
|
1003
|
+
#
|
1004
|
+
# puts bucket.put('logs/today/1.log', 2.hour)
|
1005
|
+
#
|
1006
|
+
def put(key, meta_headers={}, expires=nil, headers={})
|
1007
|
+
meta = RightAws::S3::Key.add_meta_prefix(meta_headers)
|
1008
|
+
@s3.interface.put_link(@name, key.to_s, nil, expires, meta.merge(headers))
|
1009
|
+
end
|
1010
|
+
|
1011
|
+
# Generate link to GET key data.
|
1012
|
+
#
|
1013
|
+
# bucket.get('logs/today/1.log', 1.hour)
|
1014
|
+
#
|
1015
|
+
def get(key, expires=nil, headers={})
|
1016
|
+
@s3.interface.get_link(@name, key.to_s, expires, headers)
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
# Generate link to delete bucket.
|
1020
|
+
#
|
1021
|
+
# bucket.delete(2.hour)
|
1022
|
+
#
|
1023
|
+
def delete(expires=nil, headers={})
|
1024
|
+
@s3.interface.delete_bucket_link(@name, expires, headers)
|
1025
|
+
end
|
1026
|
+
end
|
1027
|
+
|
1028
|
+
|
1029
|
+
class Key
|
1030
|
+
attr_reader :bucket, :name
|
1031
|
+
|
1032
|
+
def to_s
|
1033
|
+
@name
|
1034
|
+
end
|
1035
|
+
|
1036
|
+
# Return a full S# name (bucket/key).
|
1037
|
+
#
|
1038
|
+
# key.full_name #=> 'my_awesome_bucket/cool_key'
|
1039
|
+
#
|
1040
|
+
def full_name(separator='/')
|
1041
|
+
"#{@bucket.to_s}#{separator}#{@name}"
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
# Return a public link to key.
|
1045
|
+
#
|
1046
|
+
# key.public_link #=> 'https://s3.amazonaws.com:443/my_awesome_bucket/cool_key'
|
1047
|
+
#
|
1048
|
+
def public_link
|
1049
|
+
params = @bucket.s3.interface.params
|
1050
|
+
"#{params[:protocol]}://#{params[:server]}:#{params[:port]}/#{full_name('/')}"
|
1051
|
+
end
|
1052
|
+
|
1053
|
+
def initialize(bucket, name, meta_headers={})
|
1054
|
+
@bucket = bucket
|
1055
|
+
@name = name.to_s
|
1056
|
+
@meta_headers = meta_headers
|
1057
|
+
raise 'Key name can not be empty.' if @name.blank?
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
# Generate link to PUT key data.
|
1061
|
+
#
|
1062
|
+
# puts bucket.put('logs/today/1.log', '123', 2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=B...D&Expires=1180820032&AWSAccessKeyId=1...2
|
1063
|
+
#
|
1064
|
+
def put(expires=nil, headers={})
|
1065
|
+
@bucket.put(@name.to_s, @meta_headers, expires, headers)
|
1066
|
+
end
|
1067
|
+
|
1068
|
+
# Generate link to GET key data.
|
1069
|
+
#
|
1070
|
+
# bucket.get('logs/today/1.log', 1.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=h...M%3D&Expires=1180820032&AWSAccessKeyId=1...2
|
1071
|
+
#
|
1072
|
+
def get(expires=nil, headers={})
|
1073
|
+
@bucket.s3.interface.get_link(@bucket.to_s, @name, expires, headers)
|
1074
|
+
end
|
1075
|
+
|
1076
|
+
# Generate link to delete key.
|
1077
|
+
#
|
1078
|
+
# bucket.delete(2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=4...D&Expires=1180820032&AWSAccessKeyId=1...2
|
1079
|
+
#
|
1080
|
+
def delete(expires=nil, headers={})
|
1081
|
+
@bucket.s3.interface.delete_link(@bucket.to_s, @name, expires, headers)
|
1082
|
+
end
|
1083
|
+
|
1084
|
+
# Generate link to head key.
|
1085
|
+
#
|
1086
|
+
# bucket.head(2.hour) #=> https://s3.amazonaws.com:443/my_awesome_bucket/logs%2Ftoday%2F1.log?Signature=4...D&Expires=1180820032&AWSAccessKeyId=1...2
|
1087
|
+
#
|
1088
|
+
def head(expires=nil, headers={})
|
1089
|
+
@bucket.s3.interface.head_link(@bucket.to_s, @name, expires, headers)
|
1090
|
+
end
|
1091
|
+
end
|
1092
|
+
end
|
1093
|
+
|
1094
|
+
end
|