From ca7ec3b9b94efb92aeb30cf0d05ba0988bc6e79c Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 9 Nov 2023 16:45:00 +0800 Subject: [PATCH] add support for api_endpoint in AliyunService --- lib/active_storage/service/aliyun_service.rb | 36 +++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/active_storage/service/aliyun_service.rb b/lib/active_storage/service/aliyun_service.rb index 588a726..caa0118 100644 --- a/lib/active_storage/service/aliyun_service.rb +++ b/lib/active_storage/service/aliyun_service.rb @@ -22,7 +22,7 @@ def initialize(**config) 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: custom_metadata) do |stream| + api_bucket.put_object(path_for(key), content_type: content_type, metas: custom_metadata) do |stream| stream << io.read(CHUNK_SIZE) until io.eof? end end @@ -31,12 +31,12 @@ def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename def download(key, &block) if block instrument :streaming_download, key: key do - bucket.get_object(path_for(key), &block) + api_bucket.get_object(path_for(key), &block) end else instrument :download, key: key do chunk_buff = [] - bucket.get_object(path_for(key)) do |chunk| + api_bucket.get_object(path_for(key)) do |chunk| chunk_buff << chunk end chunk_buff.join @@ -48,7 +48,7 @@ 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| + api_bucket.get_object(path_for(key), range: [range.begin, range_end]) do |chunk| chunk_buff << chunk end chunk_buff.join @@ -57,25 +57,25 @@ def download_chunk(key, range) def delete(key) instrument :delete, key: key do - bucket.delete_object(path_for(key)) + api_bucket.delete_object(path_for(key)) end end def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do - files = bucket.list_objects(prefix: path_for(prefix)) + files = api_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) + api_bucket.batch_delete_objects(keys, quiet: true) end end def exist?(key) instrument :exist, key: key do |_payload| - bucket.object_exists?(path_for(key)) + api_bucket.object_exists?(path_for(key)) end end @@ -195,6 +195,13 @@ def bucket @bucket end + def api_bucket + return @api_bucket if defined? @api_bucket + + @api_bucket = api_client.get_bucket(config.fetch(:bucket)) + @api_bucket + end + def authorization(key, content_type, checksum, date) filename = File.expand_path("/#{bucket.name}/#{path_for(key)}") addition_headers = "x-oss-date:#{date}" @@ -207,6 +214,10 @@ def endpoint config.fetch(:endpoint, "https://oss-cn-hangzhou.aliyuncs.com") end + def api_endpoint + config.fetch(:api_endpoint, endpoint) + end + def client @client ||= Aliyun::OSS::Client.new( endpoint: endpoint, @@ -215,5 +226,14 @@ def client cname: config.fetch(:cname, false) ) end + + def api_client + @api_client ||= Aliyun::OSS::Client.new( + endpoint: api_endpoint, + access_key_id: config.fetch(:access_key_id), + access_key_secret: config.fetch(:access_key_secret), + cname: config.fetch(:cname, false) + ) + end end end