Class: CarrierWave::Nos::Bucket

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

Constant Summary collapse

PATH_PREFIX =
%r{^/}

Instance Method Summary collapse

Constructor Details

#initialize(uploader) ⇒ Bucket

Returns a new instance of Bucket.



6
7
8
9
10
11
# File 'lib/carrierwave/nos/bucket.rb', line 6

def initialize(uploader)
  @nos_access_key = uploader.nos_access_key
  @nos_secret_key = uploader.nos_secret_key
  @nos_endpoint = uploader.nos_endpoint
  @nos_bucket = uploader.nos_bucket
end

Instance Method Details

#path_to_url(path) ⇒ Object

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



32
33
34
# File 'lib/carrierwave/nos/bucket.rb', line 32

def path_to_url(path)
  "https://#{@nos_bucket}.#{@nos_endpoint}/#{path}"
end

#put(path, file) ⇒ Object

上传文件 params:

  • path - remote 存储路径

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

returns: 图片的下载地址



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/carrierwave/nos/bucket.rb', line 19

def put(path, file)
  path.sub!(PATH_PREFIX, '')

  res = oss_upload_client.put_file(file, path)

  if res.code == 200
    path_to_url(path)
  else
    raise 'Put file failed'
  end
end