google_storage 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,16 @@
2
2
  module GoogleStorage
3
3
  class Client
4
4
 
5
- def list_buckets(options={})
5
+ ###
6
+ #
7
+ # <b>Lists all buckets available within your project</b>
8
+ #
9
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#getservice
10
+ #
11
+ ###
12
+
13
+ def list_buckets
14
+ options = {}
6
15
  options[:send_goog_project_id] = true
7
16
  resp = get(nil, '/', options)
8
17
  resp_obj = Crack::XML.parse(resp.body)
@@ -15,12 +24,24 @@ module GoogleStorage
15
24
  return resp_obj
16
25
  end
17
26
 
18
- def list_acls_for_bucket(bucket, options={})
19
- resp = get(bucket, '/?acl', options)
27
+ ###
28
+ #
29
+ # <b>Lists the ACL that has been applied to a bucket</b>
30
+ #
31
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#getbucket
32
+ #
33
+ # Example:
34
+ #
35
+ # client.bucket_acls('bucket_name')
36
+ #
37
+ ###
38
+
39
+ def bucket_acls(bucket_name, options={})
40
+ resp = get(bucket_name, '/?acl', options)
20
41
  resp_obj = Crack::XML.parse(resp.body)
21
42
  if resp_obj["AccessControlList"]
22
43
  resp_obj[:success] = true
23
- resp_obj[:bucket_name] = bucket
44
+ resp_obj[:bucket_name] = bucket_name
24
45
  resp_obj[:acl] = resp_obj["AccessControlList"]
25
46
  resp_obj[:raw] = Crack::XML.parse(resp.body)
26
47
  resp_obj.each_key {|key| resp_obj.delete(key) unless key == :success || key == :bucket_name || key == :acl || key == :raw }
@@ -28,27 +49,56 @@ module GoogleStorage
28
49
  return resp_obj
29
50
  end
30
51
 
31
- alias :bucket_acls :list_acls_for_bucket
52
+ ###
53
+ #
54
+ # <b>Creates a new bucket for your project and applies the 'project-private' ACL by default</b>
55
+ #
56
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#putbucket
57
+ #
58
+ # You can apply a different ACL to a bucket by passing in an :x_goog_acl option and applying one of the predefined ACL's
59
+ #
60
+ # Example:
61
+ #
62
+ # client.create_bucket('bucket_name') <-- private bucket
63
+ # client.create_bucket('bucket_name', :x_goog_acl => 'public-read') <-- public readable bucket
64
+ #
65
+ # Available Options:
66
+ #
67
+ # :x_goog_acl => 'public-read'
68
+ #
69
+ ###
32
70
 
33
- def create_bucket(bucket, options={})
71
+ def create_bucket(bucket_name, options={})
34
72
  options[:send_goog_project_id] = true
35
- resp = put(bucket, '/', options)
73
+ resp = put(bucket_name, '/', options)
36
74
  resp_obj = Crack::XML.parse(resp.body)
37
75
  if resp.code == "200"
38
76
  resp_obj.clear
39
77
  resp_obj[:success] = true
40
- resp_obj[:bucket_name] = bucket
78
+ resp_obj[:bucket_name] = bucket_name
41
79
  resp_obj[:message] = "Bucket created"
42
80
  end
43
81
  return resp_obj
44
82
  end
45
83
 
46
- def get_bucket(bucket, options={})
47
- resp = get(bucket, '/', options)
84
+ ###
85
+ #
86
+ # <b>Returns a list of all Objects within a specified bucket</b>
87
+ #
88
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#getbucket
89
+ #
90
+ # Example:
91
+ #
92
+ # client.get_bucket('bucket_name')
93
+ #
94
+ ###
95
+
96
+ def get_bucket(bucket_name, options={})
97
+ resp = get(bucket_name, '/', options)
48
98
  resp_obj = Crack::XML.parse(resp.body)
49
99
  if resp.code == "200"
50
100
  resp_obj[:success] = true
51
- resp_obj[:bucket_name] = bucket
101
+ resp_obj[:bucket_name] = bucket_name
52
102
  contents = resp_obj["ListBucketResult"]["Contents"] ? Array.new : nil
53
103
  resp_obj["ListBucketResult"]["Contents"].is_a?(Array) ? \
54
104
  (contents = resp_obj["ListBucketResult"]["Contents"]) : \
@@ -60,14 +110,26 @@ module GoogleStorage
60
110
  return resp_obj
61
111
  end
62
112
 
63
- alias :bucket_contents :get_bucket
113
+ ###
114
+ #
115
+ # <b>Deletes a specified bucket from your project</b>
116
+ #
117
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#deletebucket
118
+ #
119
+ # *Note:* You can only delete an empty bucket
120
+ #
121
+ # Example:
122
+ #
123
+ # client.delete_bucket('bucket_name')
124
+ #
125
+ ###
64
126
 
65
- def delete_bucket(bucket, options={})
66
- resp = delete(bucket, '/', options)
127
+ def delete_bucket(bucket_name, options={})
128
+ resp = delete(bucket_name, '/', options)
67
129
  return Crack::XML.parse(resp.body) unless resp.code == "204"
68
130
  resp_obj = {}
69
131
  resp_obj[:success] = true
70
- resp_obj[:bucket_name] = bucket
132
+ resp_obj[:bucket_name] = bucket_name
71
133
  resp_obj[:message] = "Bucket deleted"
72
134
  return resp_obj
73
135
  end
@@ -5,8 +5,60 @@ require 'google_storage/object'
5
5
  require 'yaml'
6
6
 
7
7
  module GoogleStorage
8
+
9
+ ###
10
+ #
11
+ # ==Buckets & Objects
12
+ #
13
+ # Buckets are the basic containers that hold all of your data. There is only one Google namespace so every bucket across the
14
+ # entire namespace has to be uniquely named so you may find that some bucket names have already been taken.
15
+ #
16
+ # You can have multiple folders and files within a bucket but you can't nest buckets inside of each other.
17
+ #
18
+ # Objects are basically just another name for Files, stored within Google Storage. Google Storage stores Objects in 2 parts,
19
+ # holding both the object data and the object metadata. The metadata just holds key value data that describes the objects
20
+ # properties.
21
+ #
22
+ # ==Predefined ACL's (Access Control Lists)
23
+ #
24
+ # ACL's allow you to control permission settings on Objects and Buckets
25
+ #
26
+ # Google Storage uses access control lists (ACLs) to manage object and bucket access.
27
+ # ACLs are the mechanism you use to share objects with other users and allow other users to access your buckets and objects.
28
+ #
29
+ # At the moment, this google_storage gem only supports the following pre-defined ACL's. I'll add support for custom ACL's soon.
30
+ #
31
+ # project-private :: Gives permission to the project team based on their roles. Anyone who is part of the team has READ permission and project owners and project editors have FULL_CONTROL permission. This is the default ACL that's applied when you create a bucket.
32
+ # private :: Gives the requester FULL_CONTROL permission for a bucket or object. This is the default ACL that's applied when you upload an object.
33
+ # public-read :: Gives the requester FULL_CONTROL permission and gives all anonymous users READ permission. When you apply this to an object, anyone on the Internet can read the object without authenticating.
34
+ # public-read-write :: Gives the requester FULL_CONTROL permission and gives all anonymous users READ and WRITE permission. This ACL applies only to buckets.
35
+ # authenticated-read :: Gives the requester FULL_CONTROL permission and gives all authenticated Google account holders READ permission.
36
+ # bucket-owner-read :: Gives the requester FULL_CONTROL permission and gives the bucket owner READ permission. This is used only with objects.
37
+ # bucket-owner-full-control :: Gives the requester FULL_CONTROL permission and gives the bucket owner FULL_CONTROL permission. This is used only with objects.
38
+ #
39
+ ###
8
40
 
9
41
  class Client
42
+
43
+ ###
44
+ #
45
+ # <b>You need to initialize a client to be able to make requests with the google_storage gem</b>
46
+ #
47
+ # Example:
48
+ #
49
+ # The following will look for google_storage.yml in your rails config directory
50
+ #
51
+ # client = GoogleStorage::Client.new
52
+ #
53
+ # Otherwise you can pass in the path to the google_storage.yml
54
+ #
55
+ # client = GoogleStorage::Client.new(:config_yml => 'C:/example_path/google_storage.yml')
56
+ #
57
+ # Other options:
58
+ #
59
+ # :debug => true <-- This will output all debug information from the HTTP requests to $stderr
60
+ #
61
+ ###
10
62
 
11
63
  def initialize(options = {})
12
64
 
@@ -50,20 +102,21 @@ Example: GoogleStorage::Client.new(:config_yml => 'path to your google storage y
50
102
  @host = options[:host] ? options[:host] : 'commondatastorage.googleapis.com'
51
103
  end
52
104
 
53
- def google_storage_id(id)
54
- case id
55
- when :you
56
- @gsid_you
57
- when :owners
58
- @gsid_owners
59
- when :editors
60
- @gsid_editors
61
- when :team
62
- @gsid_team
63
- end
64
- end
65
105
 
66
106
  private
107
+
108
+ def google_storage_id(id)
109
+ case id
110
+ when :you
111
+ @gsid_you
112
+ when :owners
113
+ @gsid_owners
114
+ when :editors
115
+ @gsid_editors
116
+ when :team
117
+ @gsid_team
118
+ end
119
+ end
67
120
 
68
121
  def get(bucket, path, options={})
69
122
  http_request(Net::HTTP::Get, bucket, path, options)
@@ -4,9 +4,25 @@ require 'digest/md5'
4
4
  module GoogleStorage
5
5
  class Client
6
6
 
7
- def get_object(bucket, filename, options={})
7
+ ###
8
+ #
9
+ # <b>Returns a Google Storage Object inside of a Hash</b>
10
+ #
11
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#getobject
12
+ #
13
+ # Example:
14
+ #
15
+ # Returns a Hash containing your object
16
+ # client.get_object('bucket_name', 'example_image.jpg')
17
+ #
18
+ # Or write the file directly to your file system
19
+ # client.get_object('bucket_name', 'example_image.jpg', :write_to_file => 'C:/example/file.jpg')
20
+ #
21
+ ###
22
+
23
+ def get_object(bucket_name, filename, options={})
8
24
  filename.gsub!(/^\//, "")
9
- resp = get(bucket, "/#{filename}", options)
25
+ resp = get(bucket_name, "/#{filename}", options)
10
26
  return Crack::XML.parse(resp.body) unless resp.code == "200"
11
27
  resp_obj = {}
12
28
  if options[:write_to_file]
@@ -30,7 +46,32 @@ module GoogleStorage
30
46
  return resp_obj
31
47
  end
32
48
 
33
- def put_object(bucket, filename, options={})
49
+ ###
50
+ #
51
+ # <b>Uploads an Object to Google Storage, or updates if using the same filename</b>
52
+ #
53
+ # If no :x_goog_acl option is supplied, a 'private' ACL is applied by default
54
+ #
55
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#putobject
56
+ #
57
+ # *Note:* If no content type is specified then Google defaults to using 'binary/octet-stream'
58
+ #
59
+ # Example:
60
+ #
61
+ # client.put_object('bucket_name', 'file.jpg', :path_to_file => 'C:/example/file.jpg')
62
+ # client.put_object('bucket_name', 'file.jpg', :data => File.read('C:/example/file.jpg'))
63
+ #
64
+ # Available Options:
65
+ #
66
+ # :x_goog_acl => 'public-read'
67
+ # :content_type => 'image/jpeg' <-- It's recommended to always include the content type
68
+ # :path_to_file => 'path_to_file_you_want_to_upload'
69
+ # :data => [binary_data]
70
+ #
71
+ #
72
+ ###
73
+
74
+ def put_object(bucket_name, filename, options={})
34
75
  filename.gsub!(/^\//, "")
35
76
  if options[:path_to_file]
36
77
  begin
@@ -46,23 +87,37 @@ module GoogleStorage
46
87
  end
47
88
  options[:data] = @data
48
89
  end
49
- resp = put(bucket, "/#{filename}", options)
90
+ resp = put(bucket_name, "/#{filename}", options)
50
91
  public_file = (options[:x_goog_acl] && options[:x_goog_acl].match(/public/))
51
92
  return Crack::XML.parse(resp.body) unless resp.code == "200"
52
93
  resp_obj = {}
53
94
  resp_obj[:success] = true
54
95
  resp_obj[:message] = "Object added successfully"
55
96
  resp_obj[:filename] = filename
56
- resp_obj[:content_type] = options[:content_type]
57
- resp_obj[:url] = public_file ? "http://#{@host}/#{bucket}/#{filename}" : \
58
- "https://sandbox.google.com/storage/#{bucket}/#{filename}"
97
+ resp_obj[:content_type] = options[:content_type] ? options[:content_type] : 'binary/octet-stream'
98
+ resp_obj[:url] = public_file ? "http://#{@host}/#{bucket_name}/#{filename}" : \
99
+ "https://sandbox.google.com/storage/#{bucket_name}/#{filename}"
59
100
  resp_obj[:url_type] = public_file ? "public" : "private"
60
101
  return resp_obj
61
102
  end
62
103
 
63
- def list_acls_for_object(bucket, filename, options={})
104
+ alias :upload_object :put_object
105
+
106
+ ###
107
+ #
108
+ # <b>Lists the ACL that has been applied to a particular object</b>
109
+ #
110
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#getobject
111
+ #
112
+ # Example:
113
+ #
114
+ # client.object_acls('bucket_name', 'file.jpg')
115
+ #
116
+ ###
117
+
118
+ def object_acls(bucket_name, filename, options={})
64
119
  filename.gsub!(/^\//, "")
65
- resp = get(bucket, "/#{filename}?acl", options)
120
+ resp = get(bucket_name, "/#{filename}?acl", options)
66
121
  resp_obj = Crack::XML.parse(resp.body)
67
122
  if resp_obj["AccessControlList"]
68
123
  resp_obj[:success] = true
@@ -74,11 +129,21 @@ module GoogleStorage
74
129
  return resp_obj
75
130
  end
76
131
 
77
- alias :object_acls :list_acls_for_object
132
+ ###
133
+ #
134
+ # <b>Returns the metadata of an Object stored</b>
135
+ #
136
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#headobject
137
+ #
138
+ # Example:
139
+ #
140
+ # client.object_head('bucket_name', 'file.jpg')
141
+ #
142
+ ###
78
143
 
79
- def head_object(bucket, filename, options={})
144
+ def object_head(bucket_name, filename, options={})
80
145
  filename.gsub!(/^\//, "")
81
- resp = head(bucket, "/#{filename}", options)
146
+ resp = head(bucket_name, "/#{filename}", options)
82
147
  return resp.header unless resp.code == "200"
83
148
  resp_obj = {}
84
149
  resp_obj[:success] = true
@@ -92,11 +157,21 @@ module GoogleStorage
92
157
  return resp_obj
93
158
  end
94
159
 
95
- alias :object_head :head_object
160
+ ###
161
+ #
162
+ # <b>Deletes an Object from your bucket</b>
163
+ #
164
+ # Google Ref: http://code.google.com/apis/storage/docs/reference-methods.html#deleteobject
165
+ #
166
+ # Example:
167
+ #
168
+ # client.delete_object('bucket_name', 'file.jpg')
169
+ #
170
+ ###
96
171
 
97
- def delete_object(bucket, filename, options={})
172
+ def delete_object(bucket_name, filename, options={})
98
173
  filename.gsub!(/^\//, "")
99
- resp = delete(bucket, "/#{filename}", options)
174
+ resp = delete(bucket_name, "/#{filename}", options)
100
175
  return Crack::XML.parse(resp.body) unless resp.code == "204"
101
176
  resp_obj = {}
102
177
  resp_obj[:success] = true
@@ -4,6 +4,8 @@ require 'cgi'
4
4
  module GoogleStorage
5
5
  class Client
6
6
 
7
+ protected
8
+
7
9
  def construct_post_request(host, path, headers={}, params={}, options={})
8
10
  headers["Host"] = host
9
11
  headers["Date"] = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
@@ -16,7 +18,7 @@ module GoogleStorage
16
18
  raise "\nYou need to acquire a refresh_token before you can make requests to the Google API\n" unless @refresh_token
17
19
  headers["Host"] = host
18
20
  headers["Date"] = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
19
- headers["Content-Type"] = options[:content_type] ? options[:content_type] : 'application/x-www-form-urlencoded'
21
+ headers["Content-Type"] = options[:content_type] ? options[:content_type] : 'binary/octet-stream'
20
22
  headers["Content-Length"] = (options[:data] ? options[:data].size : 0).to_s
21
23
  headers["x-goog-api-version"] = @api_version
22
24
  headers["x-goog-project-id"] = @project_id if options[:send_goog_project_id]
@@ -32,50 +34,51 @@ module GoogleStorage
32
34
 
33
35
  _http_request(host, path, method, headers, param_string, options[:data])
34
36
  end
35
-
37
+
36
38
  private
37
- def _post_http_request(host, path, params, headers, data=nil)
38
- http = Net::HTTP.new(host, 443)
39
- http.use_ssl = true
40
- http.set_debug_output $stderr if @debug
41
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
42
39
 
43
- data ||= params_to_data_string(params)
44
- resp = http.post(path, data, headers)
40
+ def _post_http_request(host, path, params, headers, data=nil)
41
+ http = Net::HTTP.new(host, 443)
42
+ http.use_ssl = true
43
+ http.set_debug_output $stderr if @debug
44
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
45
45
 
46
- return resp.body
47
- end
46
+ data ||= params_to_data_string(params)
47
+ resp = http.post(path, data, headers)
48
48
 
49
+ return resp.body
50
+ end
49
51
 
50
- def _http_request(host, path, method, headers, param_string, data=nil)
51
- http = Net::HTTP.new(host, 443)
52
- http.use_ssl = true
53
- http.set_debug_output $stderr if @debug
54
- http.read_timeout = @timeout ? @timeout : 15
55
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
56
-
57
- req = method.new(path + param_string)
58
- headers.each do |key, value|
59
- req[key.to_s] = value
60
- end
61
52
 
62
- response = http.start { http.request(req, data) }
63
- return response
64
- rescue Timeout::Error
65
- $stderr.puts "Timeout accessing #{path}: #{$!}"
66
- nil
67
- rescue
68
- $stderr.puts "Error accessing #{path}: #{$!}"
69
- nil
70
- end
53
+ def _http_request(host, path, method, headers, param_string, data=nil)
54
+ http = Net::HTTP.new(host, 443)
55
+ http.use_ssl = true
56
+ http.set_debug_output $stderr if @debug
57
+ http.read_timeout = @timeout ? @timeout : 15
58
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
71
59
 
72
- def params_to_data_string(params)
73
- return "" if params.empty?
74
- esc_params = params.collect do |p|
75
- encoded = (CGI::escape(p[0].to_s) + "=" + CGI::escape(p[1].to_s))
76
- encoded.gsub('+', '%20')
60
+ req = method.new(path + param_string)
61
+ headers.each do |key, value|
62
+ req[key.to_s] = value
77
63
  end
78
- "#{esc_params.join('&')}"
64
+
65
+ response = http.start { http.request(req, data) }
66
+ return response
67
+ rescue Timeout::Error
68
+ $stderr.puts "Timeout accessing #{path}: #{$!}"
69
+ nil
70
+ rescue
71
+ $stderr.puts "Error accessing #{path}: #{$!}"
72
+ nil
73
+ end
74
+
75
+ def params_to_data_string(params)
76
+ return "" if params.empty?
77
+ esc_params = params.collect do |p|
78
+ encoded = (CGI::escape(p[0].to_s) + "=" + CGI::escape(p[1].to_s))
79
+ encoded.gsub('+', '%20')
79
80
  end
81
+ "#{esc_params.join('&')}"
82
+ end
80
83
  end
81
84
  end
@@ -22,6 +22,8 @@ module GoogleStorage
22
22
  "Failed to acquire a refresh token. Something went wrong. Try getting a new Auth code."
23
23
  end
24
24
 
25
+ protected
26
+
25
27
  def refresh_access_token(token, options={})
26
28
  options['grant_type'] = 'refresh_token'
27
29
  post_request('accounts.google.com', '/o/oauth2/token', token, options)
@@ -1,3 +1,3 @@
1
1
  module GoogleStorage
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,20 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-24 00:00:00.000000000 +10:00
13
- default_executable:
12
+ date: 2011-06-28 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: crack
17
- requirement: &9643716 !ruby/object:Gem::Requirement
16
+ requirement: &10204272 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
- - - ~>
19
+ - - ! '>='
21
20
  - !ruby/object:Gem::Version
22
- version: 0.1.1
21
+ version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *9643716
24
+ version_requirements: *10204272
26
25
  description: A Ruby client library for using the new Google Storage API v2 using OAuth2.0
27
26
  email:
28
27
  - lucas@lucashills.com
@@ -32,9 +31,27 @@ extra_rdoc_files:
32
31
  - README.textile
33
32
  files:
34
33
  - .gitignore
34
+ - .yardopts
35
35
  - Gemfile
36
36
  - README.textile
37
37
  - Rakefile
38
+ - doc/GoogleStorage.html
39
+ - doc/GoogleStorage/Client.html
40
+ - doc/GoogleStorage/InstallGenerator.html
41
+ - doc/_index.html
42
+ - doc/class_list.html
43
+ - doc/css/common.css
44
+ - doc/css/full_list.css
45
+ - doc/css/style.css
46
+ - doc/file.README.html
47
+ - doc/file_list.html
48
+ - doc/frames.html
49
+ - doc/index.html
50
+ - doc/js/app.js
51
+ - doc/js/full_list.js
52
+ - doc/js/jquery.js
53
+ - doc/method_list.html
54
+ - doc/top-level-namespace.html
38
55
  - examples/examples.rb
39
56
  - google_storage.gemspec
40
57
  - lib/generators/google_storage/install/install_generator.rb
@@ -47,7 +64,6 @@ files:
47
64
  - lib/google_storage/request.rb
48
65
  - lib/google_storage/token.rb
49
66
  - lib/google_storage/version.rb
50
- has_rdoc: true
51
67
  homepage: https://github.com/2potatocakes/google_storage
52
68
  licenses: []
53
69
  post_install_message:
@@ -68,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
84
  version: '0'
69
85
  requirements: []
70
86
  rubyforge_project:
71
- rubygems_version: 1.6.2
87
+ rubygems_version: 1.8.5
72
88
  signing_key:
73
89
  specification_version: 3
74
90
  summary: Google Storage for Developers is a RESTful service for storing and accessing