rails 中如何正确调用七牛的fetch api?【已解决】

七牛fetch api:

POST /fetch/<EncodedURL>/to/<EncodedEntryURI> HTTP/1.1
####请问Host指的是七牛绑定的域名吗?
Host:           iovip.qbox.me
Content-Type:   application/x-www-form-urlencoded
Authorization:  QBox <AccessToken>

rails 代码如下:

from_url = Qiniu::Utils.urlsafe_base64_encode('https://sf-static.b0.upaiyun.com/v-5732db60/global/img/user-256.png')
to_url = Qiniu::Utils.encode_entry_uri('weixinpic',"xxx.png")
path = "/fetch/#{from_url}/to/#{to_url}"
signing_str = path+"\n"
encoded_sign = HMAC::SHA1.digest(signing_str,Qiniu::Config.settings[:secret_key])
encoded_sign_base64 = Qiniu::Utils.urlsafe_base64_encode(encoded_sign)
authorization = "QBox #{Qiniu::Config.settings[:access_key]}:#{encoded_sign_base64}"
####http://7xtx75.com1.z0.glb.clouddn.com 是七牛绑定的域名,不知是否为fetch api的Host?
result = HTTParty.post("http://7xtx75.com1.z0.glb.clouddn.com/fetch/#{from_url}/to/#{to_url}",
                            :headers => { 'Content-Type' => 'application/x-www-form-urlencoded','Authorization' =>authorization } )

返回结果result:

{"error":"Document not found"}

from_url地址是存在的
请大家帮忙看一下,需要如何修改

正确修改后:

from_url = Qiniu::Utils.urlsafe_base64_encode('https://sf-static.b0.upaiyun.com/v-5732db60/global/img/user-256.png')
to_url = Qiniu::Utils.encode_entry_uri('weixinpic',"xxx.png")
path = "/fetch/#{from_url}/to/#{to_url}"
signing_str = path+"\n"
access_token = Qiniu::Auth.generate_acctoken(path)
authorization = "QBox #{access_token}"
host = 'iovip.qbox.me'
result = HTTParty.post("http://#{host}/fetch/#{from_url}/to/#{to_url}",
                            :headers => { 'Content-Type' => 'application/x-www-form-urlencoded','Authorization' =>authorization } )

阅读 2.9k
1 个回答

赞!已收藏,有空测试下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
七牛云问答
子站问答
访问
宣传栏