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

fetch



90
91
92
93
94
95
96
97
98
# File 'lib/qiniu/management.rb', line 90

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



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

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

.batch_delete(bucket, keys) ⇒ Object

batch_move



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

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

.batch_get(bucket, keys) ⇒ Object

batch



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

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

.batch_move(*args) ⇒ Object

batch_copy



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

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

.batch_stat(bucket, keys) ⇒ Object

batch_get



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

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

.fetch(url, bucket, key) ⇒ Object

list



85
86
87
88
# File 'lib/qiniu/management.rb', line 85

def fetch(bucket, target_url, key)
  url = Config.settings[:fetch_host] + '/fetch/' + Utils.urlsafe_base64_encode(target_url) + '/to/' + 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



127
128
129
130
# File 'lib/qiniu/management.rb', line 127

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/qiniu/management.rb', line 132

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



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

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_buffer_with_put_policy(put_policy, buf, key = nil, x_vars = nil, opts = {}) ⇒ Object

upload_with_put_policy



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/qiniu/upload.rb', line 152

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

  return upload_buffer_with_token(uptoken, buf, key, x_vars, opts)
end

.upload_buffer_with_token(uptoken, buf, key = nil, x_vars = nil, opts = {}) ⇒ Object

upload_with_token_2



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/qiniu/upload.rb', line 85

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

  ### 构造 HTTP Body
  if buf.is_a?(String)
    data = StringIO.new(buf)
  elsif buf.respond_to?(:read)
    data = buf
  end

  data.define_singleton_method("path") do
    'NO-PATH'
  end
  data.define_singleton_method("original_filename") do
    'A-MASS-OF-DATA'
  end
  data.define_singleton_method("content_type") do
    (opts[:content_type].nil? || opts[:content_type].empty?) ? 'application/octet-stream' : opts[:content_type]
  end

  post_data = {
    :file      => data,
    :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

.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不一致



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/qiniu/upload.rb', line 139

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



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

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



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
82
83
# File 'lib/qiniu/upload.rb', line 49

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