jmstacey-ruby-cloudfiles 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Unless otherwise noted, all files are released under the MIT license, exceptions contain licensing information in them.
2
+
3
+ Copyright (C) 2008 Rackspace US, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ Except as contained in this notice, the name of Rackspace US, Inc. shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Rackspace US, Inc.
12
+
data/README.markdown ADDED
@@ -0,0 +1,78 @@
1
+ Mosso Cloud Files
2
+ =================
3
+
4
+ This is a Ruby interface into the Rackspace[http://rackspace.com/] {Mosso Cloud Files}[http://www.mosso.com/cloudfiles.jsp] service. Cloud Files is reliable, scalable and affordable web-based storage hosting for backing up and archiving all your static content. Cloud Files is the first and only cloud service that leverages a tier one CDN provider to create such an easy and complete storage-to-delivery solution for media content.
5
+
6
+ Important Notice
7
+ ----------------
8
+
9
+ This is NOT the official Ruby Cloud Files API. There are two branches in this project: offical and master. The official branch contains an unmodified official 1.3.0 Ruby API released by Rackspace. This branch (master) contains a slightly enhanced version as indicated in the Enhancements section below.
10
+
11
+ * Releases from the official branch will be tagged with a prefix of "official-"
12
+ * The Ruby Gem is the latest tagged version of the enhanced version.
13
+
14
+ Requirements
15
+ ----------------
16
+
17
+ * mime-types
18
+ * archive-tar-minitar
19
+ * nokogiri
20
+ * hoe
21
+ * rcov
22
+
23
+ Installation
24
+ ---------------
25
+
26
+ * gem sources -a http://gems.github.com
27
+ * sudo gem install jmstacey-ruby-cloudfiles
28
+
29
+ Examples
30
+ ----------------
31
+
32
+ See the class definitions for documentation on specific methods and operations.
33
+
34
+ require 'cloudfiles'
35
+
36
+ # Log into the Cloud Files system
37
+ cf = CloudFiles::Connection.new(USERNAME, API_KEY)
38
+
39
+ # Get a listing of all containers under this account
40
+ cf.containers
41
+ => ["backup", "Books", "cftest", "test", "video", "webpics"]
42
+
43
+ # Access a specific container
44
+ container = cf.container('test')
45
+
46
+ # See how many objects are under this container
47
+ container.count
48
+ => 3
49
+
50
+ # List the objects
51
+ container.objects
52
+ => ["bigfile.txt", "new.txt", "test.txt"]
53
+
54
+ # Select an object
55
+ object = container.object('test.txt')
56
+
57
+ # Get that object's data
58
+ object.data
59
+ => "This is test data"
60
+
61
+ Enhancements
62
+ ----------------
63
+
64
+ * Pipe standard input ($stdin) straight to the container. [Jon Stacey in 1.3.1]
65
+ * Pass the string "STDIN" as the data for a write action. See the CFBackup project for a real-world example.
66
+
67
+ Authors
68
+ ----------------
69
+
70
+ Initial work by Major Hayden <major.hayden@rackspace.com>
71
+
72
+ Subsequent work by H. Wade Minter <wade.minter@rackspace.com>
73
+
74
+ Further enhancements by Jon Stacey
75
+
76
+ Copyright
77
+ ----------------
78
+ Copyright (c) 2009, Rackspace US, Inc. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require './lib/cloudfiles.rb'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "ruby-cloudfiles"
9
+ gem.summary = %Q{TODO}
10
+ gem.email = "jon@jonsview.com"
11
+ gem.homepage = "http://github.com/jmstacey/ruby-cloudfiles"
12
+ gem.authors = ["H. Wade Minter", "Rackspace Hosting", "Jon Stacey"]
13
+
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "ruby-cloudfiles #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 3
4
+ :patch: 2
data/lib/cloudfiles.rb ADDED
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # == Cloud Files API
4
+ # ==== Connects Ruby Applications to Rackspace's {Mosso Cloud Files service}[http://www.mosso.com/cloudfiles.jsp]
5
+ # Initial work by Major Hayden <major.hayden@rackspace.com>
6
+ #
7
+ # Subsequent work by H. Wade Minter <wade.minter@rackspace.com>
8
+ #
9
+ # See COPYING for license information.
10
+ # Copyright (c) 2009, Rackspace US, Inc.
11
+ # ----
12
+ #
13
+ # === Documentation & Examples
14
+ # To begin reviewing the available methods and examples, peruse the README file, or begin by looking at documentation for the
15
+ # CloudFiles::Connection class.
16
+ #
17
+ # The CloudFiles class is the base class. Not much of note happens here.
18
+ # To create a new CloudFiles connection, use the CloudFiles::Connection.new('user_name', 'api_key') method.
19
+ module CloudFiles
20
+
21
+ VERSION = '1.3.0'
22
+ require 'net/http'
23
+ require 'net/https'
24
+ require 'rexml/document'
25
+ require 'uri'
26
+ require 'digest/md5'
27
+ require 'jcode'
28
+ require 'time'
29
+ require 'rubygems'
30
+ require 'mime/types'
31
+
32
+ $KCODE = 'u'
33
+
34
+ $:.unshift(File.dirname(__FILE__))
35
+ require 'cloudfiles/authentication'
36
+ require 'cloudfiles/connection'
37
+ require 'cloudfiles/container'
38
+ require 'cloudfiles/storage_object'
39
+
40
+ end
41
+
42
+
43
+
44
+ class SyntaxException < StandardError # :nodoc:
45
+ end
46
+ class ConnectionException < StandardError # :nodoc:
47
+ end
48
+ class AuthenticationException < StandardError # :nodoc:
49
+ end
50
+ class InvalidResponseException < StandardError # :nodoc:
51
+ end
52
+ class NonEmptyContainerException < StandardError # :nodoc:
53
+ end
54
+ class NoSuchObjectException < StandardError # :nodoc:
55
+ end
56
+ class NoSuchContainerException < StandardError # :nodoc:
57
+ end
58
+ class NoSuchAccountException < StandardError # :nodoc:
59
+ end
60
+ class MisMatchedChecksumException < StandardError # :nodoc:
61
+ end
62
+ class IOException < StandardError # :nodoc:
63
+ end
64
+ class CDNNotEnabledException < StandardError # :nodoc:
65
+ end
66
+ class ObjectExistsException < StandardError # :nodoc:
67
+ end
68
+ class ExpiredAuthTokenException < StandardError # :nodoc:
69
+ end
@@ -0,0 +1,38 @@
1
+ module CloudFiles
2
+ class Authentication
3
+ # See COPYING for license information.
4
+ # Copyright (c) 2009, Rackspace US, Inc.
5
+
6
+ # Performs an authentication to the Cloud Files servers. Opens a new HTTP connection to the API server,
7
+ # sends the credentials, and looks for a successful authentication. If it succeeds, it sets the cdmmgmthost,
8
+ # cdmmgmtpath, storagehost, storagepath, authtoken, and authok variables on the connection. If it fails, it raises
9
+ # an AuthenticationException.
10
+ #
11
+ # Should probably never be called directly.
12
+ def initialize(connection)
13
+ path = '/auth'
14
+ hdrhash = { "X-Auth-User" => connection.authuser, "X-Auth-Key" => connection.authkey }
15
+ begin
16
+ server = Net::HTTP.new('api.mosso.com',443)
17
+ server.use_ssl = true
18
+ server.verify_mode = OpenSSL::SSL::VERIFY_NONE
19
+ server.start
20
+ rescue
21
+ raise ConnectionException, "Unable to connect to #{server}"
22
+ end
23
+ response = server.get(path,hdrhash)
24
+ if (response.code == "204")
25
+ connection.cdnmgmthost = URI.parse(response["x-cdn-management-url"]).host
26
+ connection.cdnmgmtpath = URI.parse(response["x-cdn-management-url"]).path
27
+ connection.storagehost = URI.parse(response["x-storage-url"]).host
28
+ connection.storagepath = URI.parse(response["x-storage-url"]).path
29
+ connection.authtoken = response["x-auth-token"]
30
+ connection.authok = true
31
+ else
32
+ connection.authtoken = false
33
+ raise AuthenticationException, "Authentication failed"
34
+ end
35
+ server.finish
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,286 @@
1
+ module CloudFiles
2
+ class Connection
3
+ # See COPYING for license information.
4
+ # Copyright (c) 2009, Rackspace US, Inc.
5
+
6
+ # Authentication key provided when the CloudFiles class was instantiated
7
+ attr_reader :authkey
8
+
9
+ # Token returned after a successful authentication
10
+ attr_accessor :authtoken
11
+
12
+ # Authentication username provided when the CloudFiles class was instantiated
13
+ attr_reader :authuser
14
+
15
+ # Hostname of the CDN management server
16
+ attr_accessor :cdnmgmthost
17
+
18
+ # Path for managing containers on the CDN management server
19
+ attr_accessor :cdnmgmtpath
20
+
21
+ # Array of requests that have been made so far
22
+ attr_reader :reqlog
23
+
24
+ # Hostname of the storage server
25
+ attr_accessor :storagehost
26
+
27
+ # Path for managing containers/objects on the storage server
28
+ attr_accessor :storagepath
29
+
30
+ # Instance variable that is set when authorization succeeds
31
+ attr_accessor :authok
32
+
33
+ # The total size in bytes under this connection
34
+ attr_reader :bytes
35
+
36
+ # The total number of containers under this connection
37
+ attr_reader :count
38
+
39
+ # Creates a new CloudFiles::Connection object. Uses CloudFiles::Authentication to perform the login for the connection.
40
+ # The authuser is the Mosso username, the authkey is the Mosso API key.
41
+ #
42
+ # Setting the optional retry_auth variable to false will cause an exception to be thrown if your authorization token expires.
43
+ # Otherwise, it will attempt to reauthenticate.
44
+ #
45
+ # This will likely be the base class for most operations.
46
+ #
47
+ # cf = CloudFiles::Connection.new(MY_USERNAME, MY_API_KEY)
48
+ def initialize(authuser,authkey,retry_auth = true)
49
+ @authuser = authuser
50
+ @authkey = authkey
51
+ @retry_auth = retry_auth
52
+ @authok = false
53
+ @http = {}
54
+ @reqlog = []
55
+ CloudFiles::Authentication.new(self)
56
+ end
57
+
58
+ # Returns true if the authentication was successful and returns false otherwise.
59
+ #
60
+ # cf.authok?
61
+ # => true
62
+ def authok?
63
+ @authok
64
+ end
65
+
66
+ # Returns an CloudFiles::Container object that can be manipulated easily. Throws a NoSuchContainerException if
67
+ # the container doesn't exist.
68
+ #
69
+ # container = cf.container('test')
70
+ # container.count
71
+ # => 2
72
+ def container(name)
73
+ CloudFiles::Container.new(self,name)
74
+ end
75
+ alias :get_container :container
76
+
77
+ # Sets instance variables for the bytes of storage used for this account/connection, as well as the number of containers
78
+ # stored under the account. Returns a hash with :bytes and :count keys, and also sets the instance variables.
79
+ #
80
+ # cf.get_info
81
+ # => {:count=>8, :bytes=>42438527}
82
+ # cf.bytes
83
+ # => 42438527
84
+ def get_info
85
+ response = cfreq("HEAD",@storagehost,@storagepath)
86
+ raise InvalidResponseException, "Unable to obtain account size" unless (response.code == "204")
87
+ @bytes = response["x-account-bytes-used"].to_i
88
+ @count = response["x-account-container-count"].to_i
89
+ {:bytes => @bytes, :count => @count}
90
+ end
91
+
92
+ # Gathers a list of the containers that exist for the account and returns the list of container names
93
+ # as an array. If no containers exist, an empty array is returned. Throws an InvalidResponseException
94
+ # if the request fails.
95
+ #
96
+ # If you supply the optional limit and marker parameters, the call will return the number of containers
97
+ # specified in limit, starting after the object named in marker.
98
+ #
99
+ # cf.containers
100
+ # => ["backup", "Books", "cftest", "test", "video", "webpics"]
101
+ #
102
+ # cf.containers(2,'cftest')
103
+ # => ["test", "video"]
104
+ def containers(limit=0,marker="")
105
+ paramarr = []
106
+ paramarr << ["limit=#{URI.encode(limit.to_s)}"] if limit.to_i > 0
107
+ paramarr << ["offset=#{URI.encode(marker.to_s)}"] unless marker.to_s.empty?
108
+ paramstr = (paramarr.size > 0)? paramarr.join("&") : "" ;
109
+ response = cfreq("GET",@storagehost,"#{@storagepath}?#{paramstr}")
110
+ return [] if (response.code == "204")
111
+ raise InvalidResponseException, "Invalid response code #{response.code}" unless (response.code == "200")
112
+ response.body.to_a.map { |x| x.chomp }
113
+ end
114
+ alias :list_containers :containers
115
+
116
+ # Retrieves a list of containers on the account along with their sizes (in bytes) and counts of the objects
117
+ # held within them. If no containers exist, an empty hash is returned. Throws an InvalidResponseException
118
+ # if the request fails.
119
+ #
120
+ # If you supply the optional limit and marker parameters, the call will return the number of containers
121
+ # specified in limit, starting after the object named in marker.
122
+ #
123
+ # cf.containers_detail
124
+ # => { "container1" => { :bytes => "36543", :count => "146" },
125
+ # "container2" => { :bytes => "105943", :count => "25" } }
126
+ def containers_detail(limit=0,marker="")
127
+ paramarr = []
128
+ paramarr << ["limit=#{URI.encode(limit.to_s)}"] if limit.to_i > 0
129
+ paramarr << ["offset=#{URI.encode(marker.to_s)}"] unless marker.to_s.empty?
130
+ paramstr = (paramarr.size > 0)? paramarr.join("&") : "" ;
131
+ response = cfreq("GET",@storagehost,"#{@storagepath}?format=xml&#{paramstr}")
132
+ return {} if (response.code == "204")
133
+ raise InvalidResponseException, "Invalid response code #{response.code}" unless (response.code == "200")
134
+ doc = REXML::Document.new(response.body)
135
+ detailhash = {}
136
+ doc.elements.each("account/container/") { |c|
137
+ detailhash[c.elements["name"].text] = { :bytes => c.elements["bytes"].text, :count => c.elements["count"].text }
138
+ }
139
+ doc = nil
140
+ return detailhash
141
+ end
142
+ alias :list_containers_info :containers_detail
143
+
144
+ # Returns true if the requested container exists and returns false otherwise.
145
+ #
146
+ # cf.container_exists?('good_container')
147
+ # => true
148
+ #
149
+ # cf.container_exists?('bad_container')
150
+ # => false
151
+ def container_exists?(containername)
152
+ response = cfreq("HEAD",@storagehost,"#{@storagepath}/#{containername}")
153
+ return (response.code == "204")? true : false ;
154
+ end
155
+
156
+ # Creates a new container and returns the CloudFiles::Container object. Throws an InvalidResponseException if the
157
+ # request fails.
158
+ #
159
+ # Slash (/) and question mark (?) are invalid characters, and will be stripped out. The container name is limited to
160
+ # 256 characters or less.
161
+ #
162
+ # container = cf.create_container('new_container')
163
+ # container.name
164
+ # => "new_container"
165
+ #
166
+ # container = cf.create_container('bad/name')
167
+ # => SyntaxException: Container name cannot contain the characters '/' or '?'
168
+ def create_container(containername)
169
+ raise SyntaxException, "Container name cannot contain the characters '/' or '?'" if containername.match(/[\/\?]/)
170
+ raise SyntaxException, "Container name is limited to 256 characters" if containername.length > 256
171
+ response = cfreq("PUT",@storagehost,"#{@storagepath}/#{containername}")
172
+ raise InvalidResponseException, "Unable to create container #{containername}" unless (response.code == "201" || response.code == "202")
173
+ CloudFiles::Container.new(self,containername)
174
+ end
175
+
176
+ # Deletes a container from the account. Throws a NonEmptyContainerException if the container still contains
177
+ # objects. Throws a NoSuchContainerException if the container doesn't exist.
178
+ #
179
+ # cf.delete_container('new_container')
180
+ # => true
181
+ #
182
+ # cf.delete_container('video')
183
+ # => NonEmptyContainerException: Container video is not empty
184
+ #
185
+ # cf.delete_container('nonexistent')
186
+ # => NoSuchContainerException: Container nonexistent does not exist
187
+ def delete_container(containername)
188
+ response = cfreq("DELETE",@storagehost,"#{@storagepath}/#{containername}")
189
+ raise NonEmptyContainerException, "Container #{containername} is not empty" if (response.code == "409")
190
+ raise NoSuchContainerException, "Container #{containername} does not exist" unless (response.code == "204")
191
+ true
192
+ end
193
+
194
+ # Gathers a list of public (CDN-enabled) containers that exist for an account and returns the list of container names
195
+ # as an array. If no containers are public, an empty array is returned. Throws a InvalidResponseException if
196
+ # the request fails.
197
+ #
198
+ # If you pass the optional argument as true, it will only show containers that are CURRENTLY being shared on the CDN,
199
+ # as opposed to the default behavior which is to show all containers that have EVER been public.
200
+ #
201
+ # cf.public_containers
202
+ # => ["video", "webpics"]
203
+ def public_containers(enabled_only = false)
204
+ paramstr = enabled_only == true ? "enabled_only=true" : ""
205
+ response = cfreq("GET",@cdnmgmthost,"#{@cdnmgmtpath}?#{paramstr}")
206
+ return [] if (response.code == "204")
207
+ raise InvalidResponseException, "Invalid response code #{response.code}" unless (response.code == "200")
208
+ response.body.to_a.map { |x| x.chomp }
209
+ end
210
+
211
+ # This method actually makes the HTTP calls out to the server
212
+ def cfreq(method,server,path,headers = {},data = nil,attempts = 0,&block) # :nodoc:
213
+ if data == "STDIN" # Hack (see next comment)
214
+ headers['Transfer-Encoding'] = "chunked"
215
+ end
216
+
217
+ start = Time.now
218
+ hdrhash = headerprep(headers)
219
+ path = URI.escape(path)
220
+ start_http(server,path,hdrhash)
221
+ request = Net::HTTP.const_get(method.to_s.capitalize).new(path,hdrhash)
222
+
223
+ # Hacked by Jon Stacey to enable piped data to be streamed
224
+ if data
225
+ if data == "STDIN"
226
+ request.body_stream = $stdin
227
+ elsif data.respond_to?(:read)
228
+ request.body_stream = data
229
+ else
230
+ request.body = data
231
+ end
232
+
233
+ unless data == "STDIN"
234
+ request.content_length = data.respond_to?(:lstat) ? data.stat.size : data.size
235
+ end
236
+
237
+ else
238
+ request.content_length = 0
239
+ end # if data
240
+
241
+ response = @http[server].request(request,&block)
242
+ raise ExpiredAuthTokenException if response.code == "401"
243
+ response
244
+ rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError
245
+ # Server closed the connection, retry
246
+ raise ConnectionException, "Unable to reconnect to #{server} after #{count} attempts" if attempts >= 5
247
+ attempts += 1
248
+ @http[server].finish
249
+ start_http(server,path,headers)
250
+ retry
251
+ rescue ExpiredAuthTokenException
252
+ raise ConnectionException, "Authentication token expired and you have requested not to retry" if @retry_auth == false
253
+ CloudFiles::Authentication.new(self)
254
+ retry
255
+ end
256
+
257
+ private
258
+
259
+ # Sets up standard HTTP headers
260
+ def headerprep(headers = {}) # :nodoc:
261
+ default_headers = {}
262
+ default_headers["X-Auth-Token"] = @authtoken if (authok? && @account.nil?)
263
+ default_headers["X-Storage-Token"] = @authtoken if (authok? && !@account.nil?)
264
+ default_headers["Connection"] = "Keep-Alive"
265
+ default_headers["User-Agent"] = "Ruby-CloudFiles/#{VERSION}"
266
+ default_headers.merge(headers)
267
+ end
268
+
269
+ # Starts (or restarts) the HTTP connection
270
+ def start_http(server,path,headers) # :nodoc:
271
+ if (@http[server].nil?)
272
+ begin
273
+ @http[server] = Net::HTTP.new(server,443)
274
+ @http[server].use_ssl = true
275
+ @http[server].verify_mode = OpenSSL::SSL::VERIFY_NONE
276
+ # @http[server].set_debug_output $stderr # For debug purposes
277
+ @http[server].start
278
+ rescue
279
+ raise ConnectionException, "Unable to connect to #{server}"
280
+ end
281
+ end
282
+ end
283
+
284
+ end
285
+
286
+ end