Class: Service::AliyunService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/aliyun_service.rb

Instance Method Summary collapse

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



33
34
35
36
37
# File 'lib/active_storage/service/aliyun_service.rb', line 33

def delete(key)
  instrument :delete, key: key do
    bucket.delete_object(path_for(key))
  end
end

#delete_prefixed(prefix) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/active_storage/service/aliyun_service.rb', line 39

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    files = bucket.list_objects(prefix: path_for(prefix))
    keys = files.map(&:key)
    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

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/active_storage/service/aliyun_service.rb', line 47

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



74
75
76
# File 'lib/active_storage/service/aliyun_service.rb', line 74

def headers_for_direct_upload(key, content_type:, checksum:, **)
  { "Content-Type" => content_type, "Content-MD5" => checksum }
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:, content_type:, disposition:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_storage/service/aliyun_service.rb', line 53

def url(key, expires_in:, filename:, content_type:, disposition:)
  instrument :url, key: key do |payload|
    generated_url = bucket.object_url(path_for(key), false, expires_in)
    generated_url.gsub('http://', 'https://')
    if filename.present? && filename.include?("x-oss-process")
      generated_url = [generated_url, filename].join("?")
    end

    payload[:url] = generated_url

    generated_url
  end
end

#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object



67
68
69
70
71
72
# File 'lib/active_storage/service/aliyun_service.rb', line 67

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
  instrument :url, key: key do |payload|
    # FIXME: to implement direct upload
    raise 'Not implement'
  end
end