我正在努力让多部分上传使用rails作为服务器来签署请求。 以下是代码段。 signature.endpoint是创建动作。
require 'base64'
require 'openssl'
require 'digest/sha1'
class WelcomeController < ApplicationController
skip_before_action :verify_authenticity_token
def success
render json: params
end
def index
end
def create
if(params[:headers])
signature = OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
key,
params[:headers].to_s).gsub("\n", "")
params[:signature]= signature
else
conditions = params[:conditions]
conds = [
{"acl" => "private"},
{"bucket" => conditions[1][:bucket]},
{"Content-Type" => conditions[2]["Content-Type"]},
{"success_action_status" => "200"},
{"key" => conditions[4][:key]},
{"x-amz-meta-qqfilename" => conditions[5]["x-amz-meta-qqfilename"]}
]
policy = Base64.encode64({ "expiration" => params[:expiration],
"conditions" => conds
}.to_json).
gsub("\n","")
signature = Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
key, policy)
).gsub("\n","")
params[:signature]= signature
params[:policy] = policy
end
render :json => params, :status => 200 and return
end
end
这适用于文件&lt; 5mb大小,即if-else块的一部分。 但是,当文件大小&gt;时,请求未正确签名。 5MB。
请建议我在这里缺少什么。
答案 0 :(得分:0)
溶液:
require 'base64'
require 'openssl'
require 'digest/sha1'
class WelcomeController < ApplicationController
skip_before_action :verify_authenticity_token
def success
render json: params
end
def index
end
def create
if(params[:headers])
logger.info params[:welcome][:headers]
signature = Base64.encode64(OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
key,
params[:welcome][:headers])).gsub("\n", "")
params[:signature]= signature
else
conditions = params[:conditions]
conds = [
{"acl" => "public-read"},
{"bucket" => conditions[1][:bucket]},
{"Content-Type" => conditions[2]["Content-Type"]},
{"success_action_status" => "200"},
{"key" => conditions[4][:key]},
{"x-amz-meta-qqfilename" => conditions[5]["x-amz-meta-qqfilename"]}
]
policy = Base64.encode64({ "expiration" => params[:expiration],
"conditions" => conds
}.to_json).
gsub("\n","")
signature = Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
key, policy)
).gsub("\n","")
params[:signature]= signature
params[:policy] = policy
end
logger.info signature
render :json => params, :status => 200 and return
end
end