Class: Service::AliyunService
- Inherits:
-
Service
- Object
- Service
- Service::AliyunService
- Defined in:
- lib/active_storage/service/aliyun_service.rb
Constant Summary collapse
- CHUNK_SIZE =
1024 * 1024
Instance Method Summary collapse
- #custom_metadata_headers(metadata) ⇒ Object
- #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:, custom_metadata: {}) ⇒ 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, custom_metadata: {}) ⇒ Object
-
#url(key, **options) ⇒ Object
Remove this in Rails 6.1, compatiable with Rails 6.0.0.
-
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ 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.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/active_storage/service/aliyun_service.rb', line 7 def initialize(**config) Aliyun::Common::Logging.set_log_file("/dev/null") @config = config @public = @config.fetch(:public, false) # Compatible with mode config if @config.fetch(:mode, nil) == "public" ActiveSupport::Deprecation.warn("mode has deprecated, and will remove in 1.1.0, use public: true instead.") @public = true end end |
Instance Method Details
#custom_metadata_headers(metadata) ⇒ Object
110 111 112 |
# File 'lib/active_storage/service/aliyun_service.rb', line 110 def () .transform_keys { |key| "x-oss-meta-#{key}" } end |
#delete(key) ⇒ Object
58 59 60 61 62 |
# File 'lib/active_storage/service/aliyun_service.rb', line 58 def delete(key) instrument :delete, key: key do bucket.delete_object(path_for(key)) end end |
#delete_prefixed(prefix) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/active_storage/service/aliyun_service.rb', line 64 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_storage/service/aliyun_service.rb', line 31 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
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_storage/service/aliyun_service.rb', line 47 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
76 77 78 79 80 |
# File 'lib/active_storage/service/aliyun_service.rb', line 76 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:, custom_metadata: {}) ⇒ Object
Headers for Direct Upload help.aliyun.com/document_detail/31951.html headers is required use x-oss-date instead
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/active_storage/service/aliyun_service.rb', line 99 def headers_for_direct_upload(key, content_type:, checksum:, custom_metadata: {}, **) 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, custom_metadata: {}) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/active_storage/service/aliyun_service.rb', line 22 def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil, custom_metadata: {}, **) instrument :upload, key: key, checksum: checksum do content_type ||= Marcel::MimeType.for(io) bucket.put_object(path_for(key), content_type: content_type, metas: ) do |stream| stream << io.read(CHUNK_SIZE) until io.eof? end end end |
#url(key, **options) ⇒ Object
Remove this in Rails 6.1, compatiable with Rails 6.0.0
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_storage/service/aliyun_service.rb', line 115 def url(key, **) instrument :url, key: key do |payload| generated_url = if public? public_url(key, **) else private_url(key, **) end payload[:url] = generated_url generated_url end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ 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: *
88 89 90 91 92 93 94 |
# File 'lib/active_storage/service/aliyun_service.rb', line 88 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) instrument :url, key: key do |payload| generated_url = bucket.object_url(path_for(key), false) payload[:url] = generated_url generated_url end end |