Class: CarrierWave::Aliyun::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/aliyun/bucket.rb

Constant Summary collapse

PATH_PREFIX =
%r{^/}.freeze
CHUNK_SIZE =
1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader) ⇒ Bucket

Returns a new instance of Bucket.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/carrierwave/aliyun/bucket.rb', line 13

def initialize(uploader)
  if uploader.aliyun_area.present?
    ActiveSupport::Deprecation.warn("config.aliyun_area will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_region` instead.")
    uploader.aliyun_region ||= uploader.aliyun_area
  end

  unless uploader.aliyun_private_read.nil?
    ActiveSupport::Deprecation.warn(%(config.aliyun_private_read will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_mode = :private` instead.))
    uploader.aliyun_mode ||= uploader.aliyun_private_read ? :private : :public
  end

  if uploader.aliyun_access_id.present?
    ActiveSupport::Deprecation.warn(%(config.aliyun_access_id will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_access_key_id` instead.))
    uploader.aliyun_access_key_id ||= uploader.aliyun_access_id
  end

  if uploader.aliyun_access_key.present?
    ActiveSupport::Deprecation.warn(%(config.aliyun_access_key will deprecation in carrierwave-aliyun 1.1.0, please use `aliyun_access_key_secret` instead.))
    uploader.aliyun_access_key_secret ||= uploader.aliyun_access_key
  end

  @access_key_id     = uploader.aliyun_access_key_id
  @access_key_secret = uploader.aliyun_access_key_secret
  @bucket            = uploader.aliyun_bucket
  @region            = uploader.aliyun_region || "cn-hangzhou"
  @mode              = (uploader.aliyun_mode || :public).to_sym

  # Host for get request
  @endpoint = "https://#{bucket}.oss-#{region}.aliyuncs.com"
  @host = uploader.aliyun_host || @endpoint

  unless @host.include?("//")
    raise "config.aliyun_host requirement include // http:// or https://, but you give: #{host}"
  end

  @get_endpoint = "https://oss-#{region}.aliyuncs.com"
  @upload_endpoint = uploader.aliyun_internal == true ? "https://oss-#{region}-internal.aliyuncs.com" : "https://oss-#{region}.aliyuncs.com"
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



9
10
11
# File 'lib/carrierwave/aliyun/bucket.rb', line 9

def access_key_id
  @access_key_id
end

#access_key_secretObject (readonly)

Returns the value of attribute access_key_secret.



9
10
11
# File 'lib/carrierwave/aliyun/bucket.rb', line 9

def access_key_secret
  @access_key_secret
end

#bucketObject (readonly)

Returns the value of attribute bucket.



9
10
11
# File 'lib/carrierwave/aliyun/bucket.rb', line 9

def bucket
  @bucket
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



11
12
13
# File 'lib/carrierwave/aliyun/bucket.rb', line 11

def endpoint
  @endpoint
end

#get_endpointObject (readonly)

Returns the value of attribute get_endpoint.



11
12
13
# File 'lib/carrierwave/aliyun/bucket.rb', line 11

def get_endpoint
  @get_endpoint
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/carrierwave/aliyun/bucket.rb', line 9

def host
  @host
end

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/carrierwave/aliyun/bucket.rb', line 9

def mode
  @mode
end

#regionObject (readonly)

Returns the value of attribute region.



9
10
11
# File 'lib/carrierwave/aliyun/bucket.rb', line 9

def region
  @region
end

#upload_endpointObject (readonly)

Returns the value of attribute upload_endpoint.



11
12
13
# File 'lib/carrierwave/aliyun/bucket.rb', line 11

def upload_endpoint
  @upload_endpoint
end

Instance Method Details

#copy_object(source, dest) ⇒ Object



78
79
80
81
82
83
# File 'lib/carrierwave/aliyun/bucket.rb', line 78

def copy_object(source, dest)
  source = source.sub(PATH_PREFIX, "")
  dest = dest.sub(PATH_PREFIX, "")

  oss_upload_client.copy_object(source, dest)
end

#delete(path) ⇒ Object

删除 Remote 的文件

params:

  • path - remote 存储路径

returns: 图片的下载地址



109
110
111
112
113
114
115
# File 'lib/carrierwave/aliyun/bucket.rb', line 109

def delete(path)
  path = path.sub(PATH_PREFIX, "")
  oss_upload_client.delete_object(path)
  path_to_url(path)
rescue StandardError => e
  raise "Delete failed: #{e}"
end

#get(path) ⇒ Object

读取文件params:

  • path - remote 存储路径

returns: file data



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/carrierwave/aliyun/bucket.rb', line 90

def get(path)
  path = path.sub(PATH_PREFIX, "")
  chunk_buff = []
  obj = oss_upload_client.get_object(path) do |chunk|
    chunk_buff << chunk
  end

  [obj, chunk_buff.join("")]
rescue StandardError => e
  raise "Get content faild: #{e}"
end

#get_url(path, private: false, thumb: nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/carrierwave/aliyun/bucket.rb', line 129

def get_url(path, private: false, thumb: nil)
  path = path.sub(PATH_PREFIX, "")

  url = if thumb&.start_with?("?")
          # foo.jpg?x-oss-process=image/resize,h_100
          parameters = { "x-oss-process" => thumb.split("=").last }
          oss_client.object_url(path, private, 15.minutes, parameters)
        else
          oss_client.object_url(path, private, 15.minutes)
        end

  unless private
    url = [url, thumb].join("") unless thumb&.start_with?("?")
  end

  url.sub(endpoint, host)
end

#head(path) ⇒ Object



147
148
149
150
# File 'lib/carrierwave/aliyun/bucket.rb', line 147

def head(path)
  path = path.sub(PATH_PREFIX, "")
  oss_upload_client.get_object(path)
end

#path_to_url(path, thumb: nil) ⇒ Object

根据配置返回完整的上传文件的访问地址



119
120
121
# File 'lib/carrierwave/aliyun/bucket.rb', line 119

def path_to_url(path, thumb: nil)
  get_url(path, thumb: thumb)
end

#private_get_url(path, thumb: nil) ⇒ Object

私有空间访问地址,会带上实时算出的 token 信息有效期 15 minutes



125
126
127
# File 'lib/carrierwave/aliyun/bucket.rb', line 125

def private_get_url(path, thumb: nil)
  get_url(path, private: true, thumb: thumb)
end

#put(path, file, content_type: "image/jpg", content_disposition: nil) ⇒ Object

上传文件params:

  • path - remote 存储路径

  • file - 需要上传文件的 File 对象

  • opts:

    • content_type - 上传文件的 MimeType,默认 ‘image/jpg`

    • content_disposition - Content-Disposition

returns: 图片的下载地址



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/carrierwave/aliyun/bucket.rb', line 61

def put(path, file, content_type: "image/jpg", content_disposition: nil)
  path = path.sub(PATH_PREFIX, "")

  headers = {}
  headers["Content-Type"] = content_type
  headers["Content-Disposition"] = content_disposition if content_disposition

  begin
    oss_upload_client.put_object(path, headers: headers) do |stream|
      stream << file.read(CHUNK_SIZE) until file.eof?
    end
    path_to_url(path)
  rescue StandardError => e
    raise "Put file failed: #{e}"
  end
end