activestorage-aliyun 0.1.1 → 0.2.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 +5 -0
- data/lib/active_storage/service/aliyun_service.rb +30 -4
- data/lib/active_storage_aliyun/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfac27721ec3ce8613444b343030117a4ddf6d28
|
4
|
+
data.tar.gz: 7ed58307e3dec06209a29436127d5f4f188dae0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dfb3e7aa65baa48d1040fcb8b11f867b1fab78a0eedb21b7ce1ca7ab37d05b0e56521c61fc5ca195f2f72adeaf6ed66b346bcca0349e9b9bf7ec1a2c7c27f46
|
7
|
+
data.tar.gz: dcf684e6ff9ffce4ef3e75ec5f7db26513cad9cf8ce45de3a68a501fd048a95365ab396185d6e6d28868d7e53f66e0f904d3676e10e69e969d4d68baf5b1e617
|
data/CHANGELOG.md
CHANGED
@@ -39,7 +39,9 @@ module ActiveStorage
|
|
39
39
|
def delete_prefixed(prefix)
|
40
40
|
instrument :delete_prefixed, prefix: prefix do
|
41
41
|
files = bucket.list_objects(prefix: path_for(prefix))
|
42
|
+
return if files.blank?
|
42
43
|
keys = files.map(&:key)
|
44
|
+
return if keys.blank?
|
43
45
|
bucket.batch_delete_objects(keys, quiet: true)
|
44
46
|
end
|
45
47
|
end
|
@@ -54,7 +56,7 @@ module ActiveStorage
|
|
54
56
|
instrument :url, key: key do |payload|
|
55
57
|
generated_url = bucket.object_url(path_for(key), false, expires_in)
|
56
58
|
generated_url.gsub('http://', 'https://')
|
57
|
-
if filename.
|
59
|
+
if filename.to_s.include?("x-oss-process")
|
58
60
|
generated_url = [generated_url, filename].join("?")
|
59
61
|
end
|
60
62
|
|
@@ -64,15 +66,31 @@ module ActiveStorage
|
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
69
|
+
# You must setup CORS on OSS control panel to allow JavaScript request from your site domain.
|
70
|
+
# https://www.alibabacloud.com/help/zh/doc-detail/31988.htm
|
71
|
+
# https://help.aliyun.com/document_detail/31925.html
|
72
|
+
# Source: *.your.host.com
|
73
|
+
# Allowed Methods: POST, PUT, HEAD
|
74
|
+
# Allowed Headers: *
|
67
75
|
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
|
68
76
|
instrument :url, key: key do |payload|
|
69
|
-
|
70
|
-
|
77
|
+
generated_url = bucket.object_url(path_for(key), false)
|
78
|
+
payload[:url] = generated_url
|
79
|
+
generated_url
|
71
80
|
end
|
72
81
|
end
|
73
82
|
|
83
|
+
# Headers for Direct Upload
|
84
|
+
# https://help.aliyun.com/document_detail/31951.html
|
85
|
+
# headers["Date"] is required use x-oss-date instead
|
74
86
|
def headers_for_direct_upload(key, content_type:, checksum:, **)
|
75
|
-
|
87
|
+
date = Time.now.httpdate
|
88
|
+
{
|
89
|
+
"Content-Type" => content_type,
|
90
|
+
"Content-MD5" => checksum,
|
91
|
+
"Authorization" => authorization(key, content_type, checksum, date),
|
92
|
+
"x-oss-date" => date,
|
93
|
+
}
|
76
94
|
end
|
77
95
|
|
78
96
|
private
|
@@ -89,6 +107,14 @@ module ActiveStorage
|
|
89
107
|
@bucket
|
90
108
|
end
|
91
109
|
|
110
|
+
def authorization(key, content_type, checksum, date)
|
111
|
+
filename = File.expand_path("/#{bucket.name}/#{path_for(key)}")
|
112
|
+
addition_headers = "x-oss-date:"+date
|
113
|
+
sign = ["PUT", checksum, content_type, date, addition_headers, filename].join("\n")
|
114
|
+
signature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', config.fetch(:access_key_secret), sign))
|
115
|
+
"OSS " + config.fetch(:access_key_id) + ":" + signature
|
116
|
+
end
|
117
|
+
|
92
118
|
def endpoint
|
93
119
|
config.fetch(:endpoint, "https://oss-cn-hangzhou.aliyuncs.com")
|
94
120
|
end
|