Class: Service::QiniuService
- Inherits:
-
Service
- Object
- Service
- Service::QiniuService
show all
- Includes:
- QiniuCommon
- Defined in:
- lib/active_storage/service/qiniu_service.rb
Overview
Wraps the Qiniu Cloud Storage as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.
Instance Attribute Summary collapse
Attributes included from QiniuCommon
#bucket, #host
Instance Method Summary
collapse
-
#delete(key) ⇒ Object
-
#delete_prefixed(prefix) ⇒ Object
-
#download(key, &block) ⇒ Object
-
#download_chunk(key, range) ⇒ Object
-
#exist?(key) ⇒ Boolean
-
#headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:) ⇒ Object
-
#initialize(host:, secret_key:, access_key:, bucket:, **options) ⇒ QiniuService
constructor
A new instance of QiniuService.
-
#method_for_direct_upload ⇒ Object
-
#upload(key, io, checksum: nil, **options) ⇒ Object
-
#url(key, **options) ⇒ Object
-
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
#file_for, #generate_uptoken, #upload_verbose
Constructor Details
#initialize(host:, secret_key:, access_key:, bucket:, **options) ⇒ QiniuService
Returns a new instance of QiniuService.
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/active_storage/service/qiniu_service.rb', line 11
def initialize(host:, secret_key:, access_key:, bucket:, **options)
@host = host
@bucket = bucket
@protocol = (options.delete(:protocol) || 'https').to_sym
@client = Qiniu.establish_connection!(
access_key: access_key,
secret_key: secret_key,
protocal: @protocal,
**options
)
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9
10
11
|
# File 'lib/active_storage/service/qiniu_service.rb', line 9
def client
@client
end
|
#protocol ⇒ Object
Returns the value of attribute protocol.
9
10
11
|
# File 'lib/active_storage/service/qiniu_service.rb', line 9
def protocol
@protocol
end
|
Instance Method Details
#delete(key) ⇒ Object
35
36
37
38
39
40
41
42
43
|
# File 'lib/active_storage/service/qiniu_service.rb', line 35
def delete(key)
instrument :delete, key: key do
begin
Qiniu::Storage.delete(bucket, key)
rescue => e
puts e.backtrace
end
end
end
|
#delete_prefixed(prefix) ⇒ Object
45
46
47
48
49
|
# File 'lib/active_storage/service/qiniu_service.rb', line 45
def delete_prefixed(prefix)
instrument :delete_prefixed, prefix: prefix do
file_for(prefix).each { |item| delete item['key'] }
end
end
|
#download(key, &block) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/active_storage/service/qiniu_service.rb', line 58
def download(key, &block)
if block_given?
instrument :streaming_download, key: key do
open(url(key, attname: key)) do |file|
while data = file.read(64.kilobytes)
yield data
end
end
end
else
instrument :download, key: key do
open(url(key, attname: key)).read
end
end
end
|
#download_chunk(key, range) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/active_storage/service/qiniu_service.rb', line 74
def download_chunk(key, range)
instrument :download_chunk, key: key, range: range do
uri = URI(url(key, attname: key))
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |client|
client.get(uri, 'Range' => "bytes=#{range.begin}-#{range.exclude_end? ? range.end - 1 : range.end}").body
end
end
end
|
#exist?(key) ⇒ Boolean
51
52
53
54
55
56
|
# File 'lib/active_storage/service/qiniu_service.rb', line 51
def exist?(key)
instrument :exist, key: key do |payload|
answer = file_for(key)
payload[:exist] = answer
end
end
|
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/active_storage/service/qiniu_service.rb', line 104
def (key, filename:, content_type:, content_length:, checksum:)
uptoken = generate_uptoken(key)
_url = url(key, filename: filename)
{
'Content-Type' => 'application/octet-stream',
'Content-MD5' => checksum,
'Authorization' => "UpToken #{uptoken}",
'Up-Token' => uptoken,
'Content-Url' => _url
}
end
|
#method_for_direct_upload ⇒ Object
116
117
118
|
# File 'lib/active_storage/service/qiniu_service.rb', line 116
def method_for_direct_upload
'POST'
end
|
#upload(key, io, checksum: nil, **options) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/active_storage/service/qiniu_service.rb', line 23
def upload(key, io, checksum: nil, **options)
instrument :upload, key: key, checksum: checksum do
begin
code, result, = upload_verbose(io, key, options)
result['key']
rescue => e
puts e.backtrace
raise ActiveStorage::IntegrityError
end
end
end
|
#url(key, **options) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/active_storage/service/qiniu_service.rb', line 83
def url(key, **options)
instrument :url, key: key do |payload|
if options[:filename].present?
options[:fop] ||= ''
options[:fop] = options[:fop] + '&' unless options[:fop].blank? || options[:fop].end_with?('&')
options[:fop] = options[:fop] + "attname=#{URI.escape(options[:filename].to_s)}"
end
url = Qiniu::Auth.authorize_download_url_2(host, key, fop: options[:fop], expires_in: options[:expires_in], schema: protocol)
payload[:url] = url
url
end
end
|
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/active_storage/service/qiniu_service.rb', line 96
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key: key do |payload|
url = Qiniu::Config.up_host(bucket) + "/mkblk/#{content_length}"
payload[:url] = url
url
end
end
|