aliyunoss 0.2.2 → 0.2.4

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: c22926d8762b8b4bf376899763a30ec4be2f719a4f304eb29012331d216c4a07
4
- data.tar.gz: b08fd3ba38eadc4c6813b074113e9e2ae4356e41236f19e961b4150c4993abf5
3
+ metadata.gz: ca7ea420b819bffafd6c076ef8d78a3ef4a204386dacdb51aadcd052f65861ea
4
+ data.tar.gz: ae333e82382f9968bf9909eea87006c1351cebe2f8a9bc1c15ed4cfaa5b4de4a
5
5
  SHA512:
6
- metadata.gz: 4f068b547a30385271f91d3f955d47a604f7ed622527d158d50cd659348243910fe0e2c7345ad481240a967846d90eea61d6cdf533537b6b4b3d0e8ec354780f
7
- data.tar.gz: e2cec40d40c0e8037755f1cadcdecb2c86652e376169901812feea4882eea0a50b15cba7542056255a3ec2dbb663f77c90b21339b277cd69e8eee07d61ecc47b
6
+ metadata.gz: 903d5beeb50a74696c7eec0b9a611307b6c7a4236a0b0298d48f4bb4edd789f36f06b96fbb48dd7279e55970297db2859c7b62a5c654bce8cb6bd6d3e27111a9
7
+ data.tar.gz: 689c7991cc61bcc2fbf46d80530457db8dc5bfeb2393d907e0bc8ca143d2e9b22406efaa17ec4fda2794dda26a8c4fce7409e5acc3a3869481f73cb5f4014100
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
  #
@@ -88,6 +88,16 @@ module Aliyun
88
88
  Aliyun::Oss::API.generate_share_url(self, path, expires_in)
89
89
  end
90
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
+
91
101
  #
92
102
  # Generate public url for path
93
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,27 @@ 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
+ @headers["x-oss-date"] = @headers["Date"]
102
+
103
+ if filename != nil
104
+ @headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
105
+ end
106
+
107
+ request = Net::HTTP.send(:const_get, 'Put').new(get_uri)
108
+ @headers.each_pair {|k,v| request[k] = v}
109
+ @headers['Authorization'] = 'OSS ' + access_key_id + ':' + signature(request)
110
+ @headers
111
+ end
112
+
92
113
  #
93
114
  # Get http header value by attribute
94
115
  #
@@ -160,6 +181,7 @@ module Aliyun
160
181
  if @queries[k] then "#{k}=#{@queries[k]}" else "#{k}" end
161
182
  end
162
183
 
184
+
163
185
  "/#{@bucket.name}#{@path}?#{array.sort.join('&')}"
164
186
  end
165
187
 
@@ -1,5 +1,5 @@
1
1
  module Aliyun
2
2
  module Oss
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.4"
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.2
4
+ version: 0.2.4
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