activestorage-aliyun 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +9 -8
- data/lib/active_storage/service/aliyun_service.rb +82 -74
- data/lib/active_storage_aliyun/railtie.rb +2 -0
- data/lib/active_storage_aliyun/version.rb +1 -1
- data/lib/activestorage-aliyun.rb +2 -0
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8321ccf6fc37943c33a6ac6058a28ecdc1b60830d0669c6cdd05dbb5d562cd79
|
4
|
+
data.tar.gz: d72dc8b28b787110497de6c0a774d6bf1488b066412fafe8d6d24782ef3ef638
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5086e941f5f4969b3cfbf5b3b329611090aa388d29dca9ab37478cf8937ccb3d8be0814aea78a6349b0fa7c4fd36e57257b86fb275f280ea01cc8d12aab5a21
|
7
|
+
data.tar.gz: 5722b12bd0c4f1b35ec9032ee470ae2dd55baa47f08663d2f7bc88c3870ee6417523f5fefd3bab9893066c6e5b84b8ef23f0a812360c297496d9ec04b133c197
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,16 +2,14 @@
|
|
2
2
|
|
3
3
|
Wraps the Aliyun OSS as an Active Storage service, use [Aliyun official Ruby SDK](https://github.com/aliyun/aliyun-oss-ruby-sdk) for upload.
|
4
4
|
|
5
|
-
|
6
|
-
[](https://badge.fury.io/rb/activestorage-aliyun) [](https://travis-ci.org/huacnlee/activestorage-aliyun) [](https://codeclimate.com/github/huacnlee/activestorage-aliyun) [](https://codecov.io/github/huacnlee/activestorage-aliyun?branch=master)
|
7
|
-
|
5
|
+
[](https://badge.fury.io/rb/activestorage-aliyun) [](https://github.com/huacnlee/activestorage-aliyun/actions?query=workflow%3Abuild)
|
8
6
|
|
9
7
|
## Installation
|
10
8
|
|
11
9
|
Add this line to your application's Gemfile:
|
12
10
|
|
13
11
|
```ruby
|
14
|
-
gem
|
12
|
+
gem "activestorage-aliyun"
|
15
13
|
```
|
16
14
|
|
17
15
|
And then execute:
|
@@ -22,6 +20,9 @@ $ bundle
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
23
|
+
> NOTE! Current document work for Rails 6.1, if you are using Rails 6.0, please visit: https://github.com/huacnlee/activestorage-aliyun/tree/v0.6.4
|
24
|
+
> You can also to use activestorage-aliyun 1.0.0 in Rails 6.0
|
25
|
+
|
25
26
|
config/storage.yml
|
26
27
|
|
27
28
|
```yml
|
@@ -56,7 +57,7 @@ aliyun:
|
|
56
57
|
```erb
|
57
58
|
Original File URL:
|
58
59
|
|
59
|
-
<%= image_tag @photo.image.
|
60
|
+
<%= image_tag @photo.image.url %>
|
60
61
|
```
|
61
62
|
|
62
63
|
Thumb with OSS image service:
|
@@ -64,7 +65,7 @@ Thumb with OSS image service:
|
|
64
65
|
```rb
|
65
66
|
class Photo < ApplicationRecord
|
66
67
|
def image_thumb_url(process)
|
67
|
-
self.image.
|
68
|
+
self.image.url(params: { "x-oss-process" => process })
|
68
69
|
end
|
69
70
|
end
|
70
71
|
```
|
@@ -72,7 +73,7 @@ end
|
|
72
73
|
And then:
|
73
74
|
|
74
75
|
```erb
|
75
|
-
<%= image_tag @photo.
|
76
|
+
<%= image_tag @photo.image_thumb_url("image/resize,h_100,w_100") %>
|
76
77
|
```
|
77
78
|
|
78
79
|
### Use for file download
|
@@ -81,7 +82,7 @@ If you want to get original filename (Include Chinese and other UTF-8 chars), fo
|
|
81
82
|
|
82
83
|
```erb
|
83
84
|
#
|
84
|
-
<%= image_tag @photo.image.
|
85
|
+
<%= image_tag @photo.image.url(disposition: :attachment) %>
|
85
86
|
```
|
86
87
|
|
87
88
|
## Contributing
|
@@ -19,10 +19,10 @@ module ActiveStorage
|
|
19
19
|
|
20
20
|
CHUNK_SIZE = 1024 * 1024
|
21
21
|
|
22
|
-
def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil)
|
22
|
+
def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil, custom_metadata: {}, **)
|
23
23
|
instrument :upload, key: key, checksum: checksum do
|
24
24
|
content_type ||= Marcel::MimeType.for(io)
|
25
|
-
bucket.put_object(path_for(key), content_type: content_type) do |stream|
|
25
|
+
bucket.put_object(path_for(key), content_type: content_type, metas: custom_metadata) do |stream|
|
26
26
|
stream << io.read(CHUNK_SIZE) until io.eof?
|
27
27
|
end
|
28
28
|
end
|
@@ -39,7 +39,7 @@ module ActiveStorage
|
|
39
39
|
bucket.get_object(path_for(key)) do |chunk|
|
40
40
|
chunk_buff << chunk
|
41
41
|
end
|
42
|
-
chunk_buff.join
|
42
|
+
chunk_buff.join
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -51,7 +51,7 @@ module ActiveStorage
|
|
51
51
|
bucket.get_object(path_for(key), range: [range.begin, range_end]) do |chunk|
|
52
52
|
chunk_buff << chunk
|
53
53
|
end
|
54
|
-
chunk_buff.join
|
54
|
+
chunk_buff.join
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -65,14 +65,16 @@ module ActiveStorage
|
|
65
65
|
instrument :delete_prefixed, prefix: prefix do
|
66
66
|
files = bucket.list_objects(prefix: path_for(prefix))
|
67
67
|
return if files.blank?
|
68
|
+
|
68
69
|
keys = files.map(&:key)
|
69
70
|
return if keys.blank?
|
71
|
+
|
70
72
|
bucket.batch_delete_objects(keys, quiet: true)
|
71
73
|
end
|
72
74
|
end
|
73
75
|
|
74
76
|
def exist?(key)
|
75
|
-
instrument :exist, key: key do |
|
77
|
+
instrument :exist, key: key do |_payload|
|
76
78
|
bucket.object_exists?(path_for(key))
|
77
79
|
end
|
78
80
|
end
|
@@ -83,7 +85,7 @@ module ActiveStorage
|
|
83
85
|
# Source: *.your.host.com
|
84
86
|
# Allowed Methods: POST, PUT, HEAD
|
85
87
|
# Allowed Headers: *
|
86
|
-
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
|
88
|
+
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
|
87
89
|
instrument :url, key: key do |payload|
|
88
90
|
generated_url = bucket.object_url(path_for(key), false)
|
89
91
|
payload[:url] = generated_url
|
@@ -94,16 +96,21 @@ module ActiveStorage
|
|
94
96
|
# Headers for Direct Upload
|
95
97
|
# https://help.aliyun.com/document_detail/31951.html
|
96
98
|
# headers["Date"] is required use x-oss-date instead
|
97
|
-
def headers_for_direct_upload(key, content_type:, checksum:, **)
|
99
|
+
def headers_for_direct_upload(key, content_type:, checksum:, custom_metadata: {}, **)
|
98
100
|
date = Time.now.httpdate
|
99
101
|
{
|
100
102
|
"Content-Type" => content_type,
|
101
103
|
"Content-MD5" => checksum,
|
102
104
|
"Authorization" => authorization(key, content_type, checksum, date),
|
103
105
|
"x-oss-date" => date,
|
106
|
+
**custom_metadata_headers(custom_metadata)
|
104
107
|
}
|
105
108
|
end
|
106
109
|
|
110
|
+
def custom_metadata_headers(metadata)
|
111
|
+
metadata.transform_keys { |key| "x-oss-meta-#{key}" }
|
112
|
+
end
|
113
|
+
|
107
114
|
# Remove this in Rails 6.1, compatiable with Rails 6.0.0
|
108
115
|
def url(key, **options)
|
109
116
|
instrument :url, key: key do |payload|
|
@@ -121,91 +128,92 @@ module ActiveStorage
|
|
121
128
|
end
|
122
129
|
|
123
130
|
private
|
124
|
-
attr_reader :config
|
125
131
|
|
126
|
-
|
127
|
-
filekey = path_for(key)
|
132
|
+
attr_reader :config
|
128
133
|
|
129
|
-
|
134
|
+
def private_url(key, expires_in: 60, filename: nil, content_type: nil, disposition: nil, params: {}, **)
|
135
|
+
filekey = path_for(key)
|
130
136
|
|
131
|
-
|
132
|
-
filename = ActiveStorage::Filename.wrap(filename)
|
133
|
-
params["response-content-disposition"] = content_disposition_with(type: disposition, filename: filename)
|
134
|
-
end
|
137
|
+
params["response-content-type"] = content_type unless content_type.blank?
|
135
138
|
|
136
|
-
|
139
|
+
if filename
|
140
|
+
filename = ActiveStorage::Filename.wrap(filename)
|
141
|
+
params["response-content-disposition"] = content_disposition_with(type: disposition, filename: filename)
|
137
142
|
end
|
138
143
|
|
139
|
-
|
140
|
-
|
141
|
-
end
|
144
|
+
object_url(filekey, sign: true, expires_in: expires_in, params: params)
|
145
|
+
end
|
142
146
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
end
|
147
|
+
def public_url(key, params: {}, **)
|
148
|
+
object_url(path_for(key), sign: false, params: params)
|
149
|
+
end
|
147
150
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
151
|
+
# Remove this in Rails 6.1, compatiable with Rails 6.0.0
|
152
|
+
def public?
|
153
|
+
@public == true
|
154
|
+
end
|
155
|
+
|
156
|
+
def path_for(key)
|
157
|
+
root_path = config.fetch(:path, nil)
|
158
|
+
full_path = if root_path.blank? || root_path == "/"
|
159
|
+
key
|
160
|
+
else
|
161
|
+
File.join(root_path, key)
|
162
|
+
end
|
163
|
+
|
164
|
+
full_path.gsub(%r{^/}, "").gsub(%r{/+}, "/")
|
165
|
+
end
|
155
166
|
|
156
|
-
|
167
|
+
def object_url(key, sign: false, expires_in: 60, params: {})
|
168
|
+
url = bucket.object_url(key, false)
|
169
|
+
unless sign
|
170
|
+
return url if params.blank?
|
171
|
+
|
172
|
+
return "#{url}?#{params.to_query}"
|
157
173
|
end
|
158
174
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
175
|
+
resource = "/#{bucket.name}/#{key}"
|
176
|
+
expires = Time.now.to_i + expires_in
|
177
|
+
query = {
|
178
|
+
"Expires" => expires,
|
179
|
+
"OSSAccessKeyId" => config.fetch(:access_key_id)
|
180
|
+
}
|
181
|
+
query.merge!(params)
|
165
182
|
|
166
|
-
|
167
|
-
expires = Time.now.to_i + expires_in
|
168
|
-
query = {
|
169
|
-
"Expires" => expires,
|
170
|
-
"OSSAccessKeyId" => config.fetch(:access_key_id)
|
171
|
-
}
|
172
|
-
query.merge!(params)
|
183
|
+
resource += "?" + params.map { |k, v| "#{k}=#{v}" }.sort.join("&") if params.present?
|
173
184
|
|
174
|
-
|
175
|
-
|
176
|
-
end
|
185
|
+
string_to_sign = ["GET", "", "", expires, resource].join("\n")
|
186
|
+
query["Signature"] = bucket.sign(string_to_sign)
|
177
187
|
|
178
|
-
|
179
|
-
|
188
|
+
[url, query.to_query].join("?")
|
189
|
+
end
|
180
190
|
|
181
|
-
|
182
|
-
|
191
|
+
def bucket
|
192
|
+
return @bucket if defined? @bucket
|
183
193
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
@bucket
|
188
|
-
end
|
194
|
+
@bucket = client.get_bucket(config.fetch(:bucket))
|
195
|
+
@bucket
|
196
|
+
end
|
189
197
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
198
|
+
def authorization(key, content_type, checksum, date)
|
199
|
+
filename = File.expand_path("/#{bucket.name}/#{path_for(key)}")
|
200
|
+
addition_headers = "x-oss-date:#{date}"
|
201
|
+
sign = ["PUT", checksum, content_type, date, addition_headers, filename].join("\n")
|
202
|
+
signature = bucket.sign(sign)
|
203
|
+
"OSS #{config.fetch(:access_key_id)}:#{signature}"
|
204
|
+
end
|
197
205
|
|
198
|
-
|
199
|
-
|
200
|
-
|
206
|
+
def endpoint
|
207
|
+
config.fetch(:endpoint, "https://oss-cn-hangzhou.aliyuncs.com")
|
208
|
+
end
|
201
209
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
+
def client
|
211
|
+
@client ||= Aliyun::OSS::Client.new(
|
212
|
+
endpoint: endpoint,
|
213
|
+
access_key_id: config.fetch(:access_key_id),
|
214
|
+
access_key_secret: config.fetch(:access_key_secret),
|
215
|
+
cname: config.fetch(:cname, false)
|
216
|
+
)
|
217
|
+
end
|
210
218
|
end
|
211
219
|
end
|
data/lib/activestorage-aliyun.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activestorage-aliyun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2018-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: aliyun-sdk
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 6.0
|
19
|
+
version: 0.6.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 6.0
|
26
|
+
version: 0.6.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 6.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 6.0.0
|
41
41
|
description: Wraps the Aliyun OSS as an Active Storage service.
|
42
42
|
email: huacnlee@gmail.com
|
43
43
|
executables: []
|
@@ -54,7 +54,7 @@ homepage: https://github.com/huacnlee/activestorage-aliyun
|
|
54
54
|
licenses:
|
55
55
|
- MIT
|
56
56
|
metadata: {}
|
57
|
-
post_install_message:
|
57
|
+
post_install_message:
|
58
58
|
rdoc_options: []
|
59
59
|
require_paths:
|
60
60
|
- lib
|
@@ -62,15 +62,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
62
|
requirements:
|
63
63
|
- - ">="
|
64
64
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
65
|
+
version: 2.5.0
|
66
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
68
|
- - ">="
|
69
69
|
- !ruby/object:Gem::Version
|
70
70
|
version: '0'
|
71
71
|
requirements: []
|
72
|
-
rubygems_version: 3.
|
73
|
-
signing_key:
|
72
|
+
rubygems_version: 3.2.3
|
73
|
+
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: Wraps the Aliyun OSS as an Active Storage service
|
76
76
|
test_files: []
|