aliyunoss 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1ab6499245a17d4022343848dea9974e95309a5748eb230ed4e576cf9239e96
4
- data.tar.gz: 6d353d67e14ebab41f7465961b02984280aa1cac08d89a2a422decd6aadfac65
3
+ metadata.gz: 433bc4b7e35cc8b1f84173e0962fbac93b4fae4b4e167b9b716e8d5f46418487
4
+ data.tar.gz: 60aa4bc582d36bf810196002be4c8152051f452de57b0ea975f6a05e6796da5e
5
5
  SHA512:
6
- metadata.gz: d89365a0762d01cadbee4fea9e9aca18674d49d8bd4c76bc9597dff91415f0b22a4967fae8f4f1b82f9021d543671612a007e648763ed4293689ca38f4a98fc4
7
- data.tar.gz: 77d5136397e45db437cfecd604e0dc2208ced0d178c1e81f17d0072796881ab7639673ca53ca5465874c753773ce3630ab61ef4b3876dbba086eb95d80fcf67a
6
+ metadata.gz: 432a817f2035de139b8f529f258fa6b575929b0fc45cc840da9fbba19a416a901ad24c5bf82dc7a60fe4eee4ade25cfed8407e3bae6c053c29fc5590dc027fd3
7
+ data.tar.gz: bbba5fa52b383f6ef157cfcc7aa4de5d57e4bace25ba79bb30e3686ec328640f3f5df86cf7296230ceabe73eae05f0c3964a79b9aa8faab960a96559c1a9d1b8
data/lib/aliyunoss/api.rb CHANGED
@@ -208,6 +208,19 @@ HERE
208
208
  Aliyun::Oss::OssRequest.new(bucket, path).url_for_sharing(expires_in)
209
209
  end
210
210
 
211
+ #
212
+ # return headers for uploading object
213
+ # options
214
+ #
215
+ def headers_for_upload(bucket, path, filename: nil,
216
+ content_type:, content_length:, checksum:, custom_metadata: {})
217
+ Aliyun::Oss::OssRequest.new(bucket, path).headers_for_write(filename: filename,
218
+ content_type: content_type,
219
+ content_length: content_length,
220
+ checksum: checksum,
221
+ custom_metadata: custom_metadata)
222
+ end
223
+
211
224
  #
212
225
  # Post Object
213
226
  #
@@ -45,9 +45,12 @@ module Aliyun
45
45
  # Download file from remote server
46
46
  #
47
47
  def download(path, options = {})
48
- Aliyun::Oss::API.get_object(self, path, options)
49
- .raise_unless(Net::HTTPOK)
50
- .body
48
+ response = Aliyun::Oss::API.get_object(self, path, options)
49
+ if response.code.to_i >= 200 and response.code.to_i < 300
50
+ response.body
51
+ else
52
+ raise OssException.new(response)
53
+ end
51
54
  end
52
55
 
53
56
  #
@@ -85,6 +88,16 @@ module Aliyun
85
88
  Aliyun::Oss::API.generate_share_url(self, path, expires_in)
86
89
  end
87
90
 
91
+ def direct_upload_headers(path, filename: nil,
92
+ content_type:, content_length:, checksum:, custom_metadata: {})
93
+ Aliyun::Oss::API.headers_for_upload(self, path,
94
+ filename: filename,
95
+ content_type: content_type,
96
+ content_length: content_length,
97
+ checksum: checksum,
98
+ custom_metadata: custom_metadata)
99
+ end
100
+
88
101
  #
89
102
  # Generate public url for path
90
103
  #
@@ -26,10 +26,10 @@ module Aliyun
26
26
  #
27
27
  def get_uri
28
28
  if @domain
29
- uri = URI("https://#{domain}/")
29
+ uri = URI("https://#{@domain}/")
30
30
  else
31
31
  if @bucket
32
- uri = URI("https://#{bucket.name}.#{bucket.location}.#{host}")
32
+ uri = URI("https://#{@bucket.name}.#{@bucket.location}.#{host}")
33
33
  else
34
34
  uri = URI("https://oss.#{host}")
35
35
  end
@@ -89,6 +89,25 @@ module Aliyun
89
89
  uri.to_s
90
90
  end
91
91
 
92
+ def headers_for_write(filename: nil, content_type:, content_length:,
93
+ checksum:, custom_metadata: {})
94
+ @headers = {
95
+ "Content-Type" => content_type,
96
+ "Content-MD5" => checksum,
97
+ "Date" => Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT'),
98
+ "Content-Length" => content_length
99
+ }
100
+
101
+ if filename != nil
102
+ @headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
103
+ end
104
+
105
+ request = Net::HTTP.send(:const_get, 'Put').new(get_uri)
106
+ @headers.each_pair {|k,v| request[k] = v}
107
+ @headers['Authorization'] = 'OSS ' + access_key_id + ':' + signature(request)
108
+ @headers
109
+ end
110
+
92
111
  #
93
112
  # Get http header value by attribute
94
113
  #
@@ -160,6 +179,7 @@ module Aliyun
160
179
  if @queries[k] then "#{k}=#{@queries[k]}" else "#{k}" end
161
180
  end
162
181
 
182
+
163
183
  "/#{@bucket.name}#{@path}?#{array.sort.join('&')}"
164
184
  end
165
185
 
@@ -1,5 +1,5 @@
1
1
  module Aliyun
2
2
  module Oss
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
@@ -41,6 +41,30 @@ describe Aliyun::Oss::Bucket do
41
41
  expect(data.length).to eq(File.open( File.join(__dir__, "png", "test-1.png")).size)
42
42
  end
43
43
 
44
+ it 'should generate direct upload headers' do
45
+ file = File.join(__dir__, 'png', 'test-1.png')
46
+ md5 = OpenSSL::Digest::MD5
47
+ checksum = Base64.encode64(md5.digest(IO.read(file))).strip
48
+ headers = @bucket.direct_upload_headers('/test-1.png',
49
+ filename: 'test-1.png',
50
+ content_type: 'image/png',
51
+ content_length: IO.read(file).bytesize,
52
+ checksum: checksum,
53
+ custom_metadata: {})
54
+
55
+ # Now let's upload using these headers
56
+ uri = URI("https://#{@bucket.name}.#{@bucket.location}.aliyuncs.com/test-1.png")
57
+ request = Net::HTTP::Put.new(uri)
58
+ headers.each_pair { |k,v| request[k] = v }
59
+ request.body = IO.read(file)
60
+
61
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
62
+ response = http.request(request)
63
+ expect(response.code.to_s).to eq('200')
64
+ end
65
+
66
+ end
67
+
44
68
  it 'should upload data to server in multipart way' do
45
69
  path = '/multi-part-test.dat'
46
70
  @bucket.multipart_upload_initiate(path)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyunoss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - yijiecc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-18 00:00:00.000000000 Z
11
+ date: 2022-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri