carrierwave-aliyun 1.1.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +18 -1
- data/lib/carrierwave/aliyun/bucket.rb +43 -50
- data/lib/carrierwave/aliyun/version.rb +1 -1
- data/lib/carrierwave/storage/aliyun.rb +3 -14
- data/lib/carrierwave/storage/aliyun_file.rb +20 -10
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd2862b19f1414efc11faf608fba6ecc0ac6a8048dd86d96cdf3ffa4b7ad3c7f
|
4
|
+
data.tar.gz: 031b8b1ad35539a5605abae8cebbb17b0b393d68cb4442d00ca1a3b2a97491c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d115a0e6cc1a3262a40a9f5a0a525249fef749e690d5ab7d8aeace8cb9a084881b20d6b0541833ac4c0dc0c47868b0e3805432cd31ff04044763f2b2f084160
|
7
|
+
data.tar.gz: d5c5f5ebb6567b441a73412b8670d73f6e0e9a67f867caeabdeea430d14e990e3f3ef7c5644ead6ae892c45feb891f62ed24e5766868381d056444ee6ab2f055
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,27 @@
|
|
1
|
+
## 1.2.3
|
2
|
+
|
3
|
+
- 将 Aliyun::OSS 原本的异常原样抛出;
|
4
|
+
- 验证对 [传输加速](https://help.aliyun.com/document_detail/131312.html) 的能力,并增加文档说明(实际上之前的版本就支持)。
|
5
|
+
|
6
|
+
## 1.2.2
|
7
|
+
|
8
|
+
- 修正 #79 某些场景中文文件名无法正确上传的问题;
|
9
|
+
- 修正 #70 上传文件以后 tmp 目录依然还残留着 cache 文件的问题;
|
10
|
+
|
11
|
+
## 1.2.1
|
12
|
+
|
13
|
+
- 实现 `size` 方法,修正更新图片数组时,新上传的图片会取代旧图片的问题。(#73)
|
14
|
+
|
15
|
+
## 1.2.0
|
16
|
+
|
17
|
+
- 整理修复 OSS 文件访问 URL 的生成方式,去掉 img host,保持和最新 OSS API 一样的逻辑。
|
18
|
+
- 生成 URL 的时候,不再强制替换为 https,保持 `aliyun_host` 的配置;
|
19
|
+
|
20
|
+
## 1.1.2
|
21
|
+
|
22
|
+
- 修正 aliyun-sdk 0.7.0 以上 Thumb URL 生成的支持;
|
23
|
+
- Requirement aliyun-sdk >= 0.7.0;
|
24
|
+
|
1
25
|
## 1.1.2
|
2
26
|
|
3
27
|
- 修正废弃调用方式,避免在 Ruby 2.7 里面出现 warning.
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
This gem adds support for [Aliyun OSS](http://oss.aliyun.com) to [CarrierWave](https://github.com/jnicklas/carrierwave/)
|
4
4
|
|
5
|
-
[![Gem Version](https://badge.fury.io/rb/carrierwave-aliyun.svg)](https://rubygems.org/gems/carrierwave-aliyun) [![
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/carrierwave-aliyun.svg)](https://rubygems.org/gems/carrierwave-aliyun) [![build](https://github.com/huacnlee/carrierwave-aliyun/workflows/build/badge.svg)](https://github.com/huacnlee/carrierwave-aliyun/actions?query=workflow%3Abuild)
|
6
6
|
|
7
7
|
> NOTE: 此 Gem 是一个 CarrierWave 的组件,你需要配合 CarrierWave 一起使用,如果你需要直接用 Aliyun OSS,可以尝试用 [aliyun-sdk](https://github.com/aliyun/aliyun-oss-ruby-sdk) 这个 Gem。
|
8
8
|
|
@@ -80,3 +80,20 @@ class AttachmentUploader < CarrierWave::Uploader::Base
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
```
|
83
|
+
|
84
|
+
## 启用全球传输加速
|
85
|
+
|
86
|
+
阿里云允许我们通过 `oss-accelerate.aliyuncs.com` 的节点来实现全球的传输加速,如果你的需要在境外的服务器传输到国内,或许需要开启这个功能。
|
87
|
+
|
88
|
+
你只需要将 CarrierWave Aliyun 的 `aliyun_region` 配置为 `accelerate` 即可。
|
89
|
+
|
90
|
+
```rb
|
91
|
+
config.aliyun_region = "accelerate"
|
92
|
+
```
|
93
|
+
|
94
|
+
### 异常解析
|
95
|
+
|
96
|
+
> 错误:OSS Transfer Acceleration is not configured on this bucket.
|
97
|
+
> 确保有开启 [传输加速](https://help.aliyun.com/document_detail/131312.html),进入 Bucket / 传输管理 / 传输加速 / 开启传输加速。
|
98
|
+
|
99
|
+
额外注意:Aliyun OSS 开启传输加速后需要 **30 分钟内全网生效**
|
@@ -3,12 +3,11 @@
|
|
3
3
|
module CarrierWave
|
4
4
|
module Aliyun
|
5
5
|
class Bucket
|
6
|
-
PATH_PREFIX = %r{^/}
|
6
|
+
PATH_PREFIX = %r{^/}.freeze
|
7
7
|
CHUNK_SIZE = 1024 * 1024
|
8
8
|
|
9
|
-
attr_reader :access_key_id, :access_key_secret, :bucket, :region, :mode, :host
|
10
|
-
|
11
|
-
attr_reader :endpoint, :img_endpoint, :upload_endpoint
|
9
|
+
attr_reader :access_key_id, :access_key_secret, :bucket, :region, :mode, :host, :endpoint, :upload_endpoint,
|
10
|
+
:get_endpoint
|
12
11
|
|
13
12
|
def initialize(uploader)
|
14
13
|
if uploader.aliyun_area.present?
|
@@ -16,7 +15,7 @@ module CarrierWave
|
|
16
15
|
uploader.aliyun_region ||= uploader.aliyun_area
|
17
16
|
end
|
18
17
|
|
19
|
-
|
18
|
+
unless uploader.aliyun_private_read.nil?
|
20
19
|
ActiveSupport::Deprecation.warn(%(config.aliyun_private_read will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_mode = :private` instead.))
|
21
20
|
uploader.aliyun_mode ||= uploader.aliyun_private_read ? :private : :public
|
22
21
|
end
|
@@ -38,15 +37,15 @@ module CarrierWave
|
|
38
37
|
@mode = (uploader.aliyun_mode || :public).to_sym
|
39
38
|
|
40
39
|
# Host for get request
|
41
|
-
@
|
40
|
+
@endpoint = "https://#{bucket}.oss-#{region}.aliyuncs.com"
|
41
|
+
@host = uploader.aliyun_host || @endpoint
|
42
42
|
|
43
43
|
unless @host.include?("//")
|
44
|
-
raise "config.aliyun_host requirement include // http:// or https://, but you give: #{
|
44
|
+
raise "config.aliyun_host requirement include // http:// or https://, but you give: #{host}"
|
45
45
|
end
|
46
46
|
|
47
|
-
@
|
48
|
-
@upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{
|
49
|
-
@img_endpoint = "https://img-#{self.region}.aliyuncs.com"
|
47
|
+
@get_endpoint = "https://oss-#{region}.aliyuncs.com"
|
48
|
+
@upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{region}-internal.aliyuncs.com" : "https://oss-#{region}.aliyuncs.com"
|
50
49
|
end
|
51
50
|
|
52
51
|
# 上传文件
|
@@ -65,14 +64,10 @@ module CarrierWave
|
|
65
64
|
headers["Content-Type"] = content_type
|
66
65
|
headers["Content-Disposition"] = content_disposition if content_disposition
|
67
66
|
|
68
|
-
|
69
|
-
|
70
|
-
stream << file.read(CHUNK_SIZE) until file.eof?
|
71
|
-
end
|
72
|
-
path_to_url(path)
|
73
|
-
rescue => e
|
74
|
-
raise "Put file failed: #{e}"
|
67
|
+
oss_upload_client.put_object(path, headers: headers) do |stream|
|
68
|
+
stream << file.read(CHUNK_SIZE) until file.eof?
|
75
69
|
end
|
70
|
+
path_to_url(path)
|
76
71
|
end
|
77
72
|
|
78
73
|
def copy_object(source, dest)
|
@@ -95,8 +90,6 @@ module CarrierWave
|
|
95
90
|
end
|
96
91
|
|
97
92
|
[obj, chunk_buff.join("")]
|
98
|
-
rescue => e
|
99
|
-
raise "Get content faild: #{e}"
|
100
93
|
end
|
101
94
|
|
102
95
|
# 删除 Remote 的文件
|
@@ -110,34 +103,34 @@ module CarrierWave
|
|
110
103
|
path = path.sub(PATH_PREFIX, "")
|
111
104
|
oss_upload_client.delete_object(path)
|
112
105
|
path_to_url(path)
|
113
|
-
rescue => e
|
114
|
-
raise "Delete failed: #{e}"
|
115
106
|
end
|
116
107
|
|
117
108
|
##
|
118
109
|
# 根据配置返回完整的上传文件的访问地址
|
119
110
|
def path_to_url(path, thumb: nil)
|
120
|
-
path
|
121
|
-
|
122
|
-
if thumb
|
123
|
-
[self.host, [path, thumb].join("")].join("/")
|
124
|
-
else
|
125
|
-
[self.host, path].join("/")
|
126
|
-
end
|
111
|
+
get_url(path, thumb: thumb)
|
127
112
|
end
|
128
113
|
|
129
114
|
# 私有空间访问地址,会带上实时算出的 token 信息
|
130
115
|
# 有效期 15 minutes
|
131
116
|
def private_get_url(path, thumb: nil)
|
117
|
+
get_url(path, private: true, thumb: thumb)
|
118
|
+
end
|
119
|
+
|
120
|
+
def get_url(path, private: false, thumb: nil)
|
132
121
|
path = path.sub(PATH_PREFIX, "")
|
133
122
|
|
134
|
-
url = if thumb
|
135
|
-
|
123
|
+
url = if thumb&.start_with?("?")
|
124
|
+
# foo.jpg?x-oss-process=image/resize,h_100
|
125
|
+
parameters = { "x-oss-process" => thumb.split("=").last }
|
126
|
+
oss_client.object_url(path, private, 15.minutes, parameters)
|
136
127
|
else
|
137
|
-
oss_client.object_url(path,
|
128
|
+
oss_client.object_url(path, private, 15.minutes)
|
138
129
|
end
|
139
130
|
|
140
|
-
url.
|
131
|
+
url = [url, thumb].join("") if !private && !thumb&.start_with?("?")
|
132
|
+
|
133
|
+
url.sub(endpoint, host)
|
141
134
|
end
|
142
135
|
|
143
136
|
def head(path)
|
@@ -145,28 +138,28 @@ module CarrierWave
|
|
145
138
|
oss_upload_client.get_object(path)
|
146
139
|
end
|
147
140
|
|
141
|
+
# list_objects for test
|
142
|
+
def list_objects(opts = {})
|
143
|
+
oss_client.list_objects(opts)
|
144
|
+
end
|
145
|
+
|
148
146
|
private
|
149
147
|
|
150
|
-
|
151
|
-
|
152
|
-
client = ::Aliyun::OSS::Client.new(endpoint: self.endpoint,
|
153
|
-
access_key_id: self.access_key_id, access_key_secret: self.access_key_secret)
|
154
|
-
@oss_client = client.get_bucket(self.bucket)
|
155
|
-
end
|
148
|
+
def oss_client
|
149
|
+
return @oss_client if defined?(@oss_client)
|
156
150
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
@img_client = client.get_bucket(self.bucket)
|
162
|
-
end
|
151
|
+
client = ::Aliyun::OSS::Client.new(endpoint: get_endpoint, access_key_id: access_key_id,
|
152
|
+
access_key_secret: access_key_secret)
|
153
|
+
@oss_client = client.get_bucket(bucket)
|
154
|
+
end
|
163
155
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
156
|
+
def oss_upload_client
|
157
|
+
return @oss_upload_client if defined?(@oss_upload_client)
|
158
|
+
|
159
|
+
client = ::Aliyun::OSS::Client.new(endpoint: upload_endpoint, access_key_id: access_key_id,
|
160
|
+
access_key_secret: access_key_secret)
|
161
|
+
@oss_upload_client = client.get_bucket(bucket)
|
162
|
+
end
|
170
163
|
end
|
171
164
|
end
|
172
165
|
end
|
@@ -37,22 +37,11 @@ module CarrierWave
|
|
37
37
|
# do nothing, because there's no such things as 'empty directory'
|
38
38
|
end
|
39
39
|
|
40
|
-
def clean_cache!(_seconds)
|
41
|
-
will_remove_keys = []
|
42
|
-
bucket.list_objects(prefix: uploader.cache_path).each do |file|
|
43
|
-
next unless file.is_a?(Object)
|
44
|
-
time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
|
45
|
-
time = Time.at(*time)
|
46
|
-
will_remove_keys << item.key if time < (Time.now.utc - seconds)
|
47
|
-
end
|
48
|
-
bucket.batch_delete_objects(will_remove_keys)
|
49
|
-
end
|
50
|
-
|
51
40
|
private
|
52
41
|
|
53
|
-
|
54
|
-
|
55
|
-
|
42
|
+
def bucket
|
43
|
+
@bucket ||= CarrierWave::Aliyun::Bucket.new(uploader)
|
44
|
+
end
|
56
45
|
end
|
57
46
|
end
|
58
47
|
end
|
@@ -3,17 +3,26 @@
|
|
3
3
|
module CarrierWave
|
4
4
|
module Storage
|
5
5
|
class AliyunFile
|
6
|
+
attr_writer :file
|
6
7
|
attr_reader :uploader, :path
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
alias filename path
|
10
|
+
alias identifier filename
|
10
11
|
|
11
12
|
def initialize(uploader, base, path)
|
12
|
-
@uploader
|
13
|
+
@uploader = uploader
|
14
|
+
@path = path
|
15
|
+
@base = base
|
13
16
|
end
|
14
17
|
|
15
|
-
def
|
16
|
-
|
18
|
+
def file
|
19
|
+
@file ||= bucket.get(path).try(:first)
|
20
|
+
end
|
21
|
+
|
22
|
+
def size
|
23
|
+
file.headers[:content_length].to_i
|
24
|
+
rescue StandardError
|
25
|
+
nil
|
17
26
|
end
|
18
27
|
|
19
28
|
def read
|
@@ -25,7 +34,7 @@ module CarrierWave
|
|
25
34
|
def delete
|
26
35
|
bucket.delete(path)
|
27
36
|
true
|
28
|
-
rescue => e
|
37
|
+
rescue StandardError => e
|
29
38
|
# If the file's not there, don't panic
|
30
39
|
puts "carrierwave-aliyun delete file failed: #{e}"
|
31
40
|
nil
|
@@ -86,7 +95,8 @@ module CarrierWave
|
|
86
95
|
|
87
96
|
def original_filename
|
88
97
|
return @original_filename if @original_filename
|
89
|
-
|
98
|
+
|
99
|
+
if @file&.respond_to?(:original_filename)
|
90
100
|
@file.original_filename
|
91
101
|
elsif path
|
92
102
|
::File.basename(path)
|
@@ -95,9 +105,9 @@ module CarrierWave
|
|
95
105
|
|
96
106
|
private
|
97
107
|
|
98
|
-
|
99
|
-
|
100
|
-
|
108
|
+
def bucket
|
109
|
+
@bucket ||= CarrierWave::Aliyun::Bucket.new(uploader)
|
110
|
+
end
|
101
111
|
end
|
102
112
|
end
|
103
113
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-aliyun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.7.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: 0.
|
40
|
+
version: 0.7.0
|
41
41
|
description: Aliyun OSS support for Carrierwave
|
42
42
|
email:
|
43
43
|
- huacnlee@gmail.com
|
@@ -57,7 +57,7 @@ homepage: https://github.com/huacnlee/carrierwave-aliyun
|
|
57
57
|
licenses:
|
58
58
|
- MIT
|
59
59
|
metadata: {}
|
60
|
-
post_install_message:
|
60
|
+
post_install_message:
|
61
61
|
rdoc_options: []
|
62
62
|
require_paths:
|
63
63
|
- lib
|
@@ -72,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
|
-
rubygems_version: 3.
|
76
|
-
signing_key:
|
75
|
+
rubygems_version: 3.2.3
|
76
|
+
signing_key:
|
77
77
|
specification_version: 4
|
78
78
|
summary: Aliyun OSS support for Carrierwave
|
79
79
|
test_files: []
|