Class: CarrierWave::Ucloud::Bucket

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

Constant Summary collapse

PATH_PREFIX =
%r{^/}

Instance Method Summary collapse

Constructor Details

#initialize(uploader) ⇒ Bucket

Returns a new instance of Bucket.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/carrierwave/ucloud/bucket.rb', line 9

def initialize(uploader)
  @ucloud_public_key             = uploader.ucloud_public_key
  @ucloud_private_key            = uploader.ucloud_private_key
  @ucloud_public_read            = uploader.ucloud_public_read
  @ucloud_bucket                 = @ucloud_public_read ? uploader.ucloud_public_bucket : uploader.ucloud_private_bucket
  @ucloud_bucket_host            = @ucloud_public_read ? uploader.ucloud_public_bucket_host : uploader.ucloud_private_bucket_host
  @ucloud_cdn_host               = @ucloud_public_read ? uploader.ucloud_public_cdn_host : uploader.ucloud_private_cdn_host
  @ucloud_private_expire_seconds = uploader.ucloud_private_expire_seconds || 300

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

Instance Method Details

#delete(path) ⇒ Object

删除文件



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/carrierwave/ucloud/bucket.rb', line 53

def delete(path)
  path.sub!(PATH_PREFIX, '')
  response = conn.delete(url(path)) do |req|
    req.headers['Authorization'] = authorization(req.method, nil, path)
  end

  if response.success?
    true
  else
    raise 'Ucloud Get File Fail'
  end
end

#get(path) ⇒ Object

读取文件



41
42
43
44
45
46
47
48
49
50
# File 'lib/carrierwave/ucloud/bucket.rb', line 41

def get(path)
  path.sub!(PATH_PREFIX, '')
  response = conn.get(url(path))

  if response.success?
    return response
  else
    raise 'Ucloud Get File Fail'
  end
end

#private_get_url(path, opts = {}) ⇒ Object

私有空间访问地址



87
88
89
# File 'lib/carrierwave/ucloud/bucket.rb', line 87

def private_get_url(path, opts = {})
  public_get_url(path, opts) + privite_get_url_auth(path)
end

#public_get_url(path, opts = {}) ⇒ Object

公开的访问地址



75
76
77
78
79
80
81
82
83
84
# File 'lib/carrierwave/ucloud/bucket.rb', line 75

def public_get_url(path, opts = {})
  path.sub!(PATH_PREFIX, '')
  url = ''
  if opts[:thumb]
    # TODO
  else
    url = [@ucloud_cdn_host, path].join('/')
  end
  url
end

#put(path, file, headers = {}) ⇒ Object

上传文件



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/carrierwave/ucloud/bucket.rb', line 24

def put(path, file, headers = {})
  path.sub!(PATH_PREFIX, '')

  response = conn.put(path, file.read) do |req|
    req.headers = headers
    token = authorization(req.method, headers['Content-Type'], path)
    req.headers['Authorization'] = token
  end

  if response.success?
    true
  else
    raise 'Ucloud上传失败'
  end
end

#url(path, opts = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/carrierwave/ucloud/bucket.rb', line 66

def url(path, opts = {})
  if @ucloud_public_read
    public_get_url(path, opts)
  else
    private_get_url(path, opts)
  end
end