This topic describes how to calculate signatures in Ruby on the server, configure upload callback, and use form upload to upload data to OSS.
Prerequisites
- The domain name of the application server is accessible over the Internet.
- The application server has Ruby 2.0 or later installed. To view the Ruby version, run the ruby -v command.
- The browser on the PC supports JavaScript.
Step 1: Configure the application server
Step 2: Configure the client
Step 3: Modify CORS configurations
When you use form upload to upload data from the client to OSS, the client includes
the Origin
header in the request and sends the request to OSS by using the browser. OSS verifies
the request message that includes the Origin
header for cross-origin resource sharing (CORS) verification. To use the POST method,
configure CORS rules for a bucket.
Step 4: Send an upload callback request
Core code analysis of the application server
The source code of the application server is used to implement signature-based upload and upload callback.
- Signature-based upload
During signature-based upload, the application server responds to the GET message that is sent from the client. An example of the snippet:
def get_token() expire_syncpoint = Time.now.to_i + $expire_time expire = Time.at(expire_syncpoint).utc.iso8601() response.headers['expire'] = expire policy_dict = {} condition_arrary = Array.new array_item = Array.new array_item.push('starts-with') array_item.push('$key') array_item.push($upload_dir) condition_arrary.push(array_item) policy_dict["conditions"] = condition_arrary policy_dict["expiration"] = expire policy = hash_to_jason(policy_dict) policy_encode = Base64.strict_encode64(policy).chomp; h = OpenSSL::HMAC.digest('sha1', $access_key_secret, policy_encode) hs = Digest::MD5.hexdigest(h) sign_result = Base64.strict_encode64(h).strip() callback_dict = {} callback_dict['callbackBodyType'] = 'application/x-www-form-urlencoded'; callback_dict['callbackBody'] = 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}'; callback_dict['callbackUrl'] = $callback_url; callback_param = hash_to_jason(callback_dict) base64_callback_body = Base64.strict_encode64(callback_param); token_dict = {} token_dict['accessid'] = $access_key_id token_dict['host'] = $host token_dict['policy'] = policy_encode token_dict['signature'] = sign_result token_dict['expire'] = expire_syncpoint token_dict['dir'] = $upload_dir token_dict['callback'] = base64_callback_body response.headers["Access-Control-Allow-Methods"] = "POST" response.headers["Access-Control-Allow-Origin"] = "*" result = hash_to_jason(token_dict) result end get '/*' do puts "********************* GET " get_token() end
- Upload callback
During upload callback, the application server responds to the POST message that is sent from OSS. An example of the snippet:
post '/*' do puts "********************* POST" pub_key_url = Base64.decode64(get_header('x-oss-pub-key-url')) pub_key = get_public_key(pub_key_url) rsa = OpenSSL::PKey::RSA.new(pub_key) authorization = Base64.decode64(get_header('authorization')) req_body = request.body.read if request.query_string.empty? then auth_str = CGI.unescape(request.path) + "\n" + req_body else auth_str = CGI.unescape(request.path) + '?' + request.query_string + "\n" + req_body end valid = rsa.public_key.verify( OpenSSL::Digest::MD5.new, authorization, auth_str) if valid #body({'Status' => 'OK'}.to_json) body(hash_to_jason({'Status' => 'OK'})) else halt 400, "Authorization failed!" end end
For more information, see the "(Optional) Step 4: Sign the callback request" section in Callback of the OSS API Reference.