class OceanPackage::Oss
Attributes
oss_bucket_name[RW]
oss bucket 名称
oss_bucket_path[RW]
oss bucket 路径
oss_endpoint[RW]
oss endpoint
Public Class Methods
new(bucket_name, bucket_path, endpoint)
click to toggle source
# File lib/ocean_package/oss.rb, line 12 def initialize(bucket_name, bucket_path, endpoint) @oss_bucket_name = bucket_name @oss_bucket_path = bucket_path @oss_endpoint = endpoint end
Public Instance Methods
check()
click to toggle source
校验
# File lib/ocean_package/oss.rb, line 33 def check oss_bucket_name_value = "#{@oss_bucket_name}" oss_bucket_path_value = "#{@oss_bucket_path}" oss_endpoint_value = "#{@oss_endpoint}" if oss_bucket_name_value.empty? || oss_bucket_path_value.empty? || oss_endpoint_value.empty? return false end return true end
fetch_file_url(name)
click to toggle source
文件 name 在 oss 上的路径
# File lib/ocean_package/oss.rb, line 89 def fetch_file_url(name) 'https://' + @oss_bucket_name + '.' + @oss_endpoint + oss_file_path(name) end
final_oss_bucket_path()
click to toggle source
最终的 bucket path
# File lib/ocean_package/oss.rb, line 19 def final_oss_bucket_path if @oss_bucket_path.end_with?('/') @oss_bucket_path else @oss_bucket_path + '/' end end
oss_file_path(name)
click to toggle source
文件在 oss 上的路径
# File lib/ocean_package/oss.rb, line 28 def oss_file_path(name) final_oss_bucket_path + name end
upload(file_path, name)
click to toggle source
上传文件到 oss file_path:文件路径 name:文件在 oss 上的名称 return: 文件的链接 url
# File lib/ocean_package/oss.rb, line 61 def upload(file_path, name) file_path_value = "#{file_path}" name_value = "#{name}" if file_path_value.empty? || name_value.empty? Log.error("oss upload file path or name is empty, please check !!!") return '' end cmd = upload_cmd(file_path, name) res = system(cmd) Log.info("oss upload result: #{res}") unless res == true Log.error("oss upload fail, please check !!!") return '' end url = fetch_file_url(name) Log.info("oss file url: #{url}") url end
upload_cmd(file_path, name)
click to toggle source
上传文件到 oss 的命令
# File lib/ocean_package/oss.rb, line 45 def upload_cmd(file_path, name) cmd = '${HOME}/ossutilmac64' + ' cp ' + file_path cmd += ' ' cmd += 'oss://' + @oss_bucket_name + oss_file_path(name) Log.divider Log.info("oss upload_cmd: #{cmd}") Log.divider cmd end