Class: Service::AliyunService
- Inherits:
-
Service
- Object
- Service
- Service::AliyunService
- Defined in:
- lib/active_storage/service/aliyun_service.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
-
#headers_for_direct_upload(key, content_type:, checksum:) ⇒ Object
Headers for Direct Upload help.aliyun.com/document_detail/31951.html headers is required use x-oss-date instead.
-
#initialize(**config) ⇒ AliyunService
constructor
A new instance of AliyunService.
- #upload(key, io, checksum: nil) ⇒ Object
- #url(key, expires_in:, filename: nil, content_type:, disposition:, params: {}) ⇒ Object
-
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
You must setup CORS on OSS control panel to allow JavaScript request from your site domain.
Constructor Details
#initialize(**config) ⇒ AliyunService
Returns a new instance of AliyunService.
6 7 8 9 |
# File 'lib/active_storage/service/aliyun_service.rb', line 6 def initialize(**config) Aliyun::Common::Logging.set_log_file("/dev/null") @config = config end |
Instance Method Details
#delete(key) ⇒ Object
43 44 45 46 47 |
# File 'lib/active_storage/service/aliyun_service.rb', line 43 def delete(key) instrument :delete, key: key do bucket.delete_object(path_for(key)) end end |
#delete_prefixed(prefix) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/active_storage/service/aliyun_service.rb', line 49 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do files = bucket.list_objects(prefix: path_for(prefix)) return if files.blank? keys = files.map(&:key) return if keys.blank? bucket.batch_delete_objects(keys, quiet: true) end end |
#download(key) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_storage/service/aliyun_service.rb', line 19 def download(key) instrument :download, key: key do chunk_buff = [] bucket.get_object(path_for(key)) do |chunk| if block_given? yield chunk else chunk_buff << chunk end end chunk_buff.join("") end end |
#download_chunk(key, range) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/active_storage/service/aliyun_service.rb', line 33 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do uri = URI(url(key, expires_in: 30.seconds, filename: ActiveStorage::Filename.new(""), content_type: "application/octet-stream", disposition: "inline")) 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
59 60 61 62 63 |
# File 'lib/active_storage/service/aliyun_service.rb', line 59 def exist?(key) instrument :exist, key: key do |payload| bucket.object_exists?(path_for(key)) end end |
#headers_for_direct_upload(key, content_type:, checksum:) ⇒ Object
Headers for Direct Upload help.aliyun.com/document_detail/31951.html headers is required use x-oss-date instead
102 103 104 105 106 107 108 109 110 |
# File 'lib/active_storage/service/aliyun_service.rb', line 102 def headers_for_direct_upload(key, content_type:, checksum:, **) date = Time.now.httpdate { "Content-Type" => content_type, "Content-MD5" => checksum, "Authorization" => (key, content_type, checksum, date), "x-oss-date" => date, } end |
#upload(key, io, checksum: nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/active_storage/service/aliyun_service.rb', line 11 def upload(key, io, checksum: nil) instrument :upload, key: key, checksum: checksum do bucket.put_object(path_for(key)) do |stream| stream << io.read end end end |
#url(key, expires_in:, filename: nil, content_type:, disposition:, params: {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_storage/service/aliyun_service.rb', line 65 def url(key, expires_in:, filename: nil, content_type:, disposition:, params: {}) instrument :url, key: key do |payload| sign = private_mode? || disposition == :attachment filekey = path_for(key) if disposition == :attachment params["response-content-type"] = content_type if content_type unless filename.is_a?(ActiveStorage::Filename) filename = ActiveStorage::Filename.new(filename) end params["response-content-disposition"] = content_disposition_with(type: disposition, filename: filename) end generated_url = object_url(filekey, sign: sign, expires_in: expires_in, params: params) payload[:url] = generated_url generated_url end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
You must setup CORS on OSS control panel to allow JavaScript request from your site domain. www.alibabacloud.com/help/zh/doc-detail/31988.htm help.aliyun.com/document_detail/31925.html Source: *.your.host.com Allowed Methods: POST, PUT, HEAD Allowed Headers: *
91 92 93 94 95 96 97 |
# File 'lib/active_storage/service/aliyun_service.rb', line 91 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) instrument :url, key: key do |payload| generated_url = bucket.object_url(path_for(key), false) payload[:url] = generated_url generated_url end end |