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, &block) ⇒ 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, content_type: nil, disposition: nil, filename: 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
47 48 49 50 51 |
# File 'lib/active_storage/service/aliyun_service.rb', line 47 def delete(key) instrument :delete, key: key do bucket.delete_object(path_for(key)) end end |
#delete_prefixed(prefix) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/active_storage/service/aliyun_service.rb', line 53 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, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_storage/service/aliyun_service.rb', line 20 def download(key, &block) if block_given? instrument :streaming_download, key: key do bucket.get_object(path_for(key), &block) end else instrument :download, key: key do chunk_buff = [] bucket.get_object(path_for(key)) do |chunk| chunk_buff << chunk end chunk_buff.join("") end end end |
#download_chunk(key, range) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_storage/service/aliyun_service.rb', line 36 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do chunk_buff = [] range_end = range.exclude_end? ? range.end : range.end + 1 bucket.get_object(path_for(key), range: [range.begin, range_end]) do |chunk| chunk_buff << chunk end chunk_buff.join("") end end |
#exist?(key) ⇒ Boolean
63 64 65 66 67 |
# File 'lib/active_storage/service/aliyun_service.rb', line 63 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
104 105 106 107 108 109 110 111 112 |
# File 'lib/active_storage/service/aliyun_service.rb', line 104 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, content_type: nil, disposition: nil, filename: nil) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/active_storage/service/aliyun_service.rb', line 11 def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil) instrument :upload, key: key, checksum: checksum do content_type ||= Marcel::MimeType.for(io) bucket.put_object(path_for(key), content_type: content_type) do |stream| stream << io.read end end end |
#url(key, expires_in:, filename: nil, content_type:, disposition:, params: {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_storage/service/aliyun_service.rb', line 69 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) params["response-content-type"] = content_type unless content_type.blank? if filename filename = ActiveStorage::Filename.wrap(filename) 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: *
93 94 95 96 97 98 99 |
# File 'lib/active_storage/service/aliyun_service.rb', line 93 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 |