aliyun-oss-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ module Aliyun
2
+ module Oss
3
+ class Http # nodoc
4
+ attr_reader :access_key, :secret_key
5
+
6
+ def initialize(access_key, secret_key, host)
7
+ @access_key = access_key
8
+ @secret_key = secret_key
9
+ @host = host
10
+ end
11
+
12
+ def get(uri, options = {})
13
+ request('GET', uri, options)
14
+ end
15
+
16
+ def put(uri, options = {})
17
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }.merge(options[:headers] || {})
18
+ request('PUT', uri, options.merge(headers: headers))
19
+ end
20
+
21
+ def post(uri, options = {})
22
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }.merge(options[:headers] || {})
23
+ request('POST', uri, options.merge(headers: headers))
24
+ end
25
+
26
+ def delete(uri, options = {})
27
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }.merge(options[:headers] || {})
28
+ request('DELETE', uri, options.merge(headers: headers))
29
+ end
30
+
31
+ def options(uri, options = {})
32
+ request('OPTIONS', uri, options)
33
+ end
34
+
35
+ def head(uri, options = {})
36
+ request('HEAD', uri, options)
37
+ end
38
+
39
+ private
40
+
41
+ def request(verb, resource, options = {})
42
+ headers = options.delete(:headers) || {}
43
+ headers = default_headers.merge!(headers)
44
+
45
+ if options[:body]
46
+ headers.merge!('Content-MD5' => Utils.md5_digest(options[:body])) unless headers.key?('Content-MD5')
47
+ headers.merge!('Content-Length' => Utils.content_size(options[:body]).to_s) unless headers.key?('Content-Length')
48
+ end
49
+
50
+ headers.merge!('Host' => get_host(options))
51
+
52
+ auth_key = get_auth_key(options.merge(verb: verb, headers: headers, date: headers['Date']))
53
+ headers.merge!('Authorization' => auth_key)
54
+
55
+ uri = get_uri(headers['Host'], resource)
56
+ options = Utils.hash_slice(options.merge(headers: headers), :query, :headers, :body)
57
+
58
+ HTTParty.__send__(verb.downcase, uri, options)
59
+ end
60
+
61
+ def get_auth_key(options)
62
+ Authorization.get_authorization(access_key, secret_key, options)
63
+ end
64
+
65
+ def default_headers
66
+ {
67
+ 'User-Agent' => "aliyun-oss-sdk-ruby/#{Aliyun::Oss::VERSION} (#{RbConfig::CONFIG['host_os']} ruby-#{RbConfig::CONFIG['ruby_version']})",
68
+ 'Date' => Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')
69
+ }
70
+ end
71
+
72
+ def get_host(options)
73
+ if options[:location] && options[:bucket]
74
+ "#{options[:bucket]}.#{options[:location]}.aliyuncs.com"
75
+ elsif options[:bucket]
76
+ "#{options[:bucket]}.#{@host}"
77
+ else
78
+ @host
79
+ end
80
+ end
81
+
82
+ def get_uri(host, resource)
83
+ "http://#{host}#{resource}"
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,32 @@
1
+ module Aliyun
2
+ module Oss
3
+ module Multipart
4
+ class Part
5
+ # [Integer] :number the part number
6
+ attr_accessor :number
7
+
8
+ # [String] :etag the etag for the part
9
+ attr_accessor :etag
10
+
11
+ def initialize(options = {})
12
+ @number = options[:number]
13
+ @etag = options[:etag]
14
+ end
15
+
16
+ def to_hash
17
+ if valid?
18
+ { 'PartNumber' => number, 'ETag' => etag }
19
+ else
20
+ {}
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def valid?
27
+ number && etag
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,61 @@
1
+ module Aliyun
2
+ module Oss
3
+ module Rule
4
+ class Cors
5
+ ACCESSPTED_METHODS = %w(GET PUT DELETE POST HEAD)
6
+
7
+ # [Array] :allowed_origins Set allowed origins
8
+ attr_accessor :allowed_origins
9
+
10
+ # [Array] :allowed_methods Set allowed methods
11
+ attr_accessor :allowed_methods
12
+
13
+ # [Array] :allowed_headers Set allowed headers used in preflight (see #bucket_preflight)
14
+ attr_accessor :allowed_headers
15
+
16
+ # [Array] :expose_headers Set allowed used response headers for user
17
+ attr_accessor :expose_headers
18
+
19
+ # [Integer] :max_age_seconds Specifies cache time the browser to pre-fetch a particular resource request in seconds
20
+ attr_accessor :max_age_seconds
21
+
22
+ def initialize(options = {})
23
+ @allowed_origins = options[:allowed_origins]
24
+ @allowed_methods = options[:allowed_methods]
25
+ @allowed_headers = options[:allowed_headers]
26
+ @expose_headers = options[:expose_headers]
27
+ @max_age_seconds = options[:max_age_seconds]
28
+ end
29
+
30
+ def to_hash
31
+ if valid?
32
+ hash = {
33
+ 'AllowedOrigin' => allowed_origins,
34
+ 'AllowedMethod' => allowed_methods
35
+ }
36
+ hash.merge!('AllowedHeader' => allowed_headers) if value_present?(allowed_headers)
37
+ hash.merge!('EsposeHeader' => expose_headers) if value_present?(expose_headers)
38
+ hash.merge!('MaxAgeSeconds' => max_age_seconds) if max_age_seconds
39
+ hash
40
+ else
41
+ {}
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def valid?
48
+ value_present?(allowed_origins) && value_present?(allowed_methods)
49
+ end
50
+
51
+ def allowed_methods
52
+ @allowed_methods.map(&:upcase).select { |method| ACCESSPTED_METHODS.include?(method.to_s) }
53
+ end
54
+
55
+ def value_present?(value)
56
+ value && !value.empty?
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,56 @@
1
+ module Aliyun
2
+ module Oss
3
+ module Rule
4
+ class LifeCycle
5
+ # [Integer] optional, Rule ID, auto set when not set
6
+ attr_accessor :id
7
+
8
+ # [String] Used for filter objects
9
+ attr_accessor :prefix
10
+
11
+ # [Boolean] Used for set rule status
12
+ attr_accessor :enable
13
+
14
+ # [Integer] Set auto delete objects after days since last modified, at least exist one with date
15
+ attr_accessor :days
16
+
17
+ # [Time] Set auto auto delete object at given time, at least exist one with days
18
+ attr_accessor :date
19
+
20
+ def initialize(options = {})
21
+ @id = options[:id]
22
+ @prefix = options[:prefix]
23
+ @enable = options[:enable]
24
+ @days = options[:days]
25
+ @date = options[:date]
26
+ end
27
+
28
+ def to_hash
29
+ if valid?
30
+ { 'ID' => id || '', 'Prefix' => prefix, 'Status' => status, 'Expiration' => expiration }
31
+ else
32
+ {}
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def valid?
39
+ prefix && (days || date)
40
+ end
41
+
42
+ def status
43
+ enable ? 'Enabled' : 'Disabled'
44
+ end
45
+
46
+ def expiration
47
+ if date && date.is_a?(Time)
48
+ { 'Date' => date.utc.strftime('%Y-%m-%dT00:00:00.000Z') }
49
+ elsif days && days.is_a?(Integer)
50
+ { 'Days' => days.to_i }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,49 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'digest'
4
+ require 'gyoku'
5
+
6
+ module Aliyun
7
+ module Oss
8
+ class Utils
9
+ # Calculate content length
10
+ #
11
+ # @return [Integer]
12
+ def self.content_size(content)
13
+ if content.respond_to?(:size)
14
+ content.size
15
+ elsif content.is_a?(IO)
16
+ content.stat.size
17
+ end
18
+ end
19
+
20
+ # Digest body with MD5 and then encoding with Base64
21
+ #
22
+ # @return [String]
23
+ def self.md5_digest(body)
24
+ Base64.encode64(Digest::MD5.digest(body)).strip
25
+ end
26
+
27
+ # @example
28
+ # Utils.hash_slice({ 'a' => 1, 'b' => 2, 'c' => 3 }, 'a', 'c') # { 'a' => 1, 'c' => 3 }
29
+ #
30
+ # @return [Hash]
31
+ def self.hash_slice(hash, *selected_keys)
32
+ new_hash = {}
33
+ selected_keys.each { |k| new_hash[k] = hash[k] if hash.key?(k) }
34
+ new_hash
35
+ end
36
+
37
+ # Convert File or Bin data to bin data
38
+ #
39
+ # @return [Bin data]
40
+ def self.to_data(file_or_bin)
41
+ file_or_bin.respond_to?(:read) ? IO.binread(file_or_bin) : file
42
+ end
43
+
44
+ def self.to_xml(hash) # nodoc
45
+ %(<?xml version="1.0" encoding="UTF-8"?>#{Gyoku.xml(hash)})
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module Aliyun
2
+ module Oss
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'gyoku'
2
+
3
+ module Aliyun
4
+ module Oss
5
+ class XmlBuilder
6
+ def self.to_xml(hash)
7
+ %(<?xml version="1.0" encoding="UTF-8"?>#{Gyoku.xml(hash)})
8
+ end
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliyun-oss-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Newell Zhu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gyoku
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: timecop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Aliyun OSS Ruby SDK
154
+ email:
155
+ - zlx.star@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".travis.yml"
162
+ - Gemfile
163
+ - README.md
164
+ - Rakefile
165
+ - aliyun-oss.gemspec
166
+ - bin/console
167
+ - bin/setup
168
+ - lib/aliyun/oss.rb
169
+ - lib/aliyun/oss/authorization.rb
170
+ - lib/aliyun/oss/client.rb
171
+ - lib/aliyun/oss/http.rb
172
+ - lib/aliyun/oss/multipart/part.rb
173
+ - lib/aliyun/oss/rule/cors.rb
174
+ - lib/aliyun/oss/rule/lifecycle.rb
175
+ - lib/aliyun/oss/utils.rb
176
+ - lib/aliyun/oss/version.rb
177
+ - lib/aliyun/oss/xml_builder.rb
178
+ homepage: http://github.com/aliyun/ruby-oss-sdk
179
+ licenses: []
180
+ metadata: {}
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubyforge_project:
197
+ rubygems_version: 2.4.5
198
+ signing_key:
199
+ specification_version: 4
200
+ summary: Aliyun OSS Ruby SDK
201
+ test_files: []
202
+ has_rdoc: