Module: Qiniu::Storage

Extended by:
Utils
Defined in:
lib/qiniu/upload.rb,
lib/qiniu/management.rb,
lib/qiniu/resumable_upload.rb

Defined Under Namespace

Modules: AbstractClass Classes: BlockProgressNotifier, ChunkProgressNotifier, FileData, ListPolicy

Class Method Summary collapse

Methods included from Utils

crc32checksum, debug, encode_entry_uri, http_request, safe_json_parse, send_request_with, urlsafe_base64_decode, urlsafe_base64_encode

Class Method Details

.batch(command, bucket, keys) ⇒ Object

delete



85
86
87
88
89
90
91
92
93
# File 'lib/qiniu/management.rb', line 85

def batch(command, bucket, keys)
  execs = []
  keys.each do |key|
    encoded_uri = encode_entry_uri(bucket, key)
    execs << "op=/#{command}/#{encoded_uri}"
  end
  url = Config.settings[:rs_host] + "/batch"
  return HTTP.management_post(url, execs.join("&"))
end

.batch_copy(*args) ⇒ Object

batch_stat



103
104
105
# File 'lib/qiniu/management.rb', line 103

def batch_copy(*args)
  _batch_cp_or_mv('copy', args)
end

.batch_delete(bucket, keys) ⇒ Object

batch_move



111
112
113
# File 'lib/qiniu/management.rb', line 111

def batch_delete(bucket, keys)
  batch("delete", bucket, keys)
end

.batch_get(bucket, keys) ⇒ Object

batch



95
96
97
# File 'lib/qiniu/management.rb', line 95

def batch_get(bucket, keys)
  batch("get", bucket, keys)
end

.batch_move(*args) ⇒ Object

batch_copy



107
108
109
# File 'lib/qiniu/management.rb', line 107

def batch_move(*args)
  _batch_cp_or_mv('move', args)
end

.batch_stat(bucket, keys) ⇒ Object

batch_get



99
100
101
# File 'lib/qiniu/management.rb', line 99

def batch_stat(bucket, keys)
  batch("stat", bucket, keys)
end

.bucketsObject



50
51
52
53
# File 'lib/qiniu/management.rb', line 50

def buckets
  url = Config.settings[:rs_host] + '/buckets'
  return HTTP.management_post(url)
end

.copy(source_bucket, source_key, target_bucket, target_key) ⇒ Object

get



68
69
70
71
72
# File 'lib/qiniu/management.rb', line 68

def copy(source_bucket, source_key, target_bucket, target_key)
  uri = _generate_cp_or_mv_opstr('copy', source_bucket, source_key, target_bucket, target_key)
  url = Config.settings[:rs_host] + uri
  return HTTP.management_post(url)
end

.delete(bucket, key) ⇒ Object

move



80
81
82
83
# File 'lib/qiniu/management.rb', line 80

def delete(bucket, key)
  url = Config.settings[:rs_host] + '/delete/' + encode_entry_uri(bucket, key)
  return HTTP.management_post(url)
end

.get(bucket, key, save_as = nil, expires_in = nil, version = nil) ⇒ Object

stat



60
61
62
63
64
65
66
# File 'lib/qiniu/management.rb', line 60

def get(bucket, key, save_as = nil, expires_in = nil, version = nil)
  url = Config.settings[:rs_host] + '/get/' + encode_entry_uri(bucket, key)
  url += '/base/' + version unless version.nil?
  url += '/attName/' + Utils.urlsafe_base64_encode(save_as) unless save_as.nil?
  url += '/expires/' + expires_in.to_s if !expires_in.nil? && expires_in > 0
  return HTTP.management_post(url)
end

.image_mogrify_save_as(bucket, key, source_image_url, options) ⇒ Object

save_as



122
123
124
125
# File 'lib/qiniu/management.rb', line 122

def image_mogrify_save_as(bucket, key, source_image_url, options)
  mogrify_params_string = Fop::Image.generate_mogrify_params_string(options)
  save_as(bucket, key, source_image_url, mogrify_params_string)
end

.list(list_policy) ⇒ Object

image_mogrify_save_as



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/qiniu/management.rb', line 127

def list(list_policy)
  url = Config.settings[:rsf_host] + '/list?' + list_policy.to_query_string()

  resp_code, resp_body, resp_headers = HTTP.management_post(url)
  if resp_code == 0 || resp_code > 299 then
    has_more = false
    return resp_code, resp_body, resp_headers, has_more, list_policy
  end

  has_more = (resp_body['marker'].is_a?(String) && resp_body['marker'] != '')
  if has_more then
    new_list_policy = list_policy.clone()
    new_list_policy.marker = resp_body['marker']
  else
    new_list_policy = list_policy
  end

  return resp_code, resp_body, resp_headers, has_more, new_list_policy
end

.move(source_bucket, source_key, target_bucket, target_key) ⇒ Object

copy



74
75
76
77
78
# File 'lib/qiniu/management.rb', line 74

def move(source_bucket, source_key, target_bucket, target_key)
  uri = _generate_cp_or_mv_opstr('move', source_bucket, source_key, target_bucket, target_key)
  url = Config.settings[:rs_host] + uri
  return HTTP.management_post(url)
end

.resumable_upload_with_token(uptoken, local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, customer = nil, callback_params = nil, rotate = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/qiniu/resumable_upload.rb', line 46

def resumable_upload_with_token(uptoken,
                      local_file,
                      bucket,
                      key = nil,
                      mime_type = nil,
                      custom_meta = nil,
                      customer = nil,
                      callback_params = nil,
                      rotate = nil)
  begin
    ifile = File.open(local_file, 'rb')
    fh = FileData.new(ifile)
    fsize = fh.data_size
    key = Digest::SHA1.hexdigest(local_file + fh.mtime.to_s) if key.nil?
    if mime_type.nil? || mime_type.empty?
      mime = MIME::Types.type_for local_file
      mime_type = mime.empty? ? 'application/octet-stream' : mime[0].content_type
    end
    code, data = _resumable_upload(uptoken, fh, fsize, bucket, key, mime_type, custom_meta, customer, callback_params, rotate)
    [code, data]
  ensure
    ifile.close unless ifile.nil?
  end
end

.save_as(bucket, key, source_url, op_params_string) ⇒ Object

batch_delete



115
116
117
118
119
120
# File 'lib/qiniu/management.rb', line 115

def save_as(bucket, key, source_url, op_params_string)
  encoded_uri = encode_entry_uri(bucket, key)
  save_as_string = '/save-as/' + encoded_uri
  new_url = source_url + '?' + op_params_string + save_as_string
  return HTTP.management_post(new_url)
end

.stat(bucket, key) ⇒ Object

buckets



55
56
57
58
# File 'lib/qiniu/management.rb', line 55

def stat(bucket, key)
  url = Config.settings[:rs_host] + '/stat/' + encode_entry_uri(bucket, key)
  return HTTP.management_post(url)
end

.upload_with_put_policy(put_policy, local_file, key = nil, x_vars = nil, opts = {}) ⇒ Object

授权举例 put_policy.bucket | put_policy.key | key | 语义 | 授权 :—————- | :————- | :—— | :— | :— trivial_bucket | <nil> | <nil> | 新增 | 允许,最终key为1)使用put_policy.save_key生成的值或2)资源内容的Hash值 trivial_bucket | <nil> | foo.txt | 新增 | 允许 trivial_bucket | <nil> | bar.jpg | 新增 | 允许 trivial_bucket | foo.txt | <nil> | 覆盖 | 允许,由SDK将put_policy.key赋值给key实现 trivial_bucket | foo.txt | foo.txt | 覆盖 | 允许 trivial_bucket | foo.txt | bar.jpg | 覆盖 | 禁止,put_policy.key与key不一致



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/qiniu/upload.rb', line 92

def upload_with_put_policy(put_policy,
                           local_file,
                           key = nil,
                           x_vars = nil,
                           opts = {})
  uptoken = Auth.generate_uptoken(put_policy)
  if key.nil? then
    key = put_policy.key
  end

  return upload_with_token_2(uptoken, local_file, key, x_vars, opts)
end

.upload_with_token(uptoken, local_file, bucket, key = nil, mime_type = nil, custom_meta = nil, callback_params = nil, enable_crc32_check = false, rotate = nil) ⇒ Object



9
10
11
12
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
# File 'lib/qiniu/upload.rb', line 9

def upload_with_token(uptoken,
                      local_file,
                      bucket,
                      key = nil,
                      mime_type = nil,
                      custom_meta = nil,
                      callback_params = nil,
                      enable_crc32_check = false,
                      rotate = nil)
  action_params = _generate_action_params(
    local_file,
    bucket,
    key,
    mime_type,
    custom_meta,
    enable_crc32_check,
    rotate
  )

  if callback_params.nil?
    callback_params = {:bucket => bucket, :key => key, :mime_type => mime_type}
  end
  callback_query_string = HTTP.generate_query_string(callback_params)

  url = Config.settings[:up_host] + '/upload'
  post_data = {
    :params     => callback_query_string,
    :action     => action_params,
    :file       => File.new(local_file, 'rb'),
    :multipart  => true
  }
  if !uptoken.nil? then
    post_data[:auth] = uptoken unless uptoken.nil?
  end

  return HTTP.api_post(url, post_data)
end

.upload_with_token_2(uptoken, local_file, key = nil, x_vars = nil, opts = {}) ⇒ Object

upload_with_token



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qiniu/upload.rb', line 47

def upload_with_token_2(uptoken,
                        local_file,
                        key = nil,
                        x_vars = nil,
                        opts = {})
  ### 构造URL
  url = Config.settings[:up_host]
  url[/\/*$/] = ''
  url += '/'

  ### 构造HTTP Body
  file = File.new(local_file, 'rb')
  if not opts[:content_type].nil?
    file.define_singleton_method("content_type") do
      opts[:content_type]
    end
  end

  post_data = {
    :file      => file,
    :multipart => true,
  }
  if not uptoken.nil?
    post_data[:token] = uptoken
  end
  if not key.nil?
    post_data[:key] = key
  end
  if x_vars.is_a?(Hash)
    post_data.merge!(x_vars)
  end

  ### 发送请求
  HTTP.api_post(url, post_data)
end