class CarrierWave::Aliyun::Bucket
Constants
- CHUNK_SIZE
- PATH_PREFIX
Attributes
access_key_id[R]
access_key_secret[R]
bucket[R]
endpoint[R]
get_endpoint[R]
host[R]
mode[R]
region[R]
upload_endpoint[R]
Public Class Methods
new(uploader)
click to toggle source
# File lib/carrierwave/aliyun/bucket.rb, line 12 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
Public Instance Methods
copy_object(source, dest)
click to toggle source
# File lib/carrierwave/aliyun/bucket.rb, line 73 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)
click to toggle source
删除 Remote 的文件
params:
-
path - remote 存储路径
returns: 图片的下载地址
# File lib/carrierwave/aliyun/bucket.rb, line 102 def delete(path) path = path.sub(PATH_PREFIX, "") oss_upload_client.delete_object(path) path_to_url(path) end
get(path)
click to toggle source
读取文件 params:
-
path - remote 存储路径
returns: file data
# File lib/carrierwave/aliyun/bucket.rb, line 85 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("")] end
get_url(path, private: false, thumb: nil)
click to toggle source
# File lib/carrierwave/aliyun/bucket.rb, line 120 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 url = [url, thumb].join("") if !private && !thumb&.start_with?("?") url.sub(endpoint, host) end
head(path)
click to toggle source
# File lib/carrierwave/aliyun/bucket.rb, line 136 def head(path) path = path.sub(PATH_PREFIX, "") oss_upload_client.get_object(path) end
list_objects(opts = {})
click to toggle source
list_objects
for test
# File lib/carrierwave/aliyun/bucket.rb, line 142 def list_objects(opts = {}) oss_client.list_objects(opts) end
path_to_url(path, thumb: nil)
click to toggle source
根据配置返回完整的上传文件的访问地址
# File lib/carrierwave/aliyun/bucket.rb, line 110 def path_to_url(path, thumb: nil) get_url(path, thumb: thumb) end
private_get_url(path, thumb: nil)
click to toggle source
私有空间访问地址,会带上实时算出的 token 信息 有效期 15 minutes
# File lib/carrierwave/aliyun/bucket.rb, line 116 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)
click to toggle source
上传文件 params:
-
path - remote 存储路径
-
file - 需要上传文件的 File 对象
-
opts:
-
content_type - 上传文件的 MimeType,默认 `image/jpg`
-
content_disposition - Content-Disposition
-
returns: 图片的下载地址
# File lib/carrierwave/aliyun/bucket.rb, line 60 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 oss_upload_client.put_object(path, headers: headers) do |stream| stream << file.read(CHUNK_SIZE) until file.eof? end path_to_url(path) end
Private Instance Methods
oss_client()
click to toggle source
# File lib/carrierwave/aliyun/bucket.rb, line 148 def oss_client return @oss_client if defined?(@oss_client) client = ::Aliyun::OSS::Client.new(endpoint: get_endpoint, access_key_id: access_key_id, access_key_secret: access_key_secret) @oss_client = client.get_bucket(bucket) end
oss_upload_client()
click to toggle source
# File lib/carrierwave/aliyun/bucket.rb, line 156 def oss_upload_client return @oss_upload_client if defined?(@oss_upload_client) client = ::Aliyun::OSS::Client.new(endpoint: upload_endpoint, access_key_id: access_key_id, access_key_secret: access_key_secret) @oss_upload_client = client.get_bucket(bucket) end