Created
April 28, 2013 02:36
-
-
Save ikbear/5475637 to your computer and use it in GitHub Desktop.
Upload a file to qiniu cloud storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'thor' | |
require 'rest_client' | |
require 'base64' | |
require 'qiniu-rs' | |
class QiniuUploader < Thor | |
desc "upload", "Upload a file to qiniu cloud storage" | |
def upload | |
bucket = 'mybucket' | |
key = 'mykey' | |
action = "/rs-put/#{urlsafe_base64_encode("#{bucket}:#{key}")}" | |
Qiniu::RS.establish_connection! :access_key => '< Please input your access key here >', :secret_key => '< Please input your secret key here >' | |
token = Qiniu::RS.generate_upload_token :expires_in => 3600*24*1000, :scpoe => bucket | |
file = '/path/to/file' | |
res = RestClient.post 'http://up.qbox.me/upload', :auth => token, :action => action, :file => File.open(file) | |
puts res.inspect | |
end | |
private | |
def urlsafe_base64_encode(s) | |
Base64.encode64(s).strip.gsub('+', '-').gsub('/','_').gsub(/\r?\n/, '') | |
end | |
end | |
QiniuUploader.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment