FineUploader在Ruby中签名

时间:2014-06-26 12:59:45

标签: ruby-on-rails ruby amazon-s3 fine-uploader

您是否有任何使用FineUploader.com在ruby中签署s3服务器的示例?我尝试了很多变种:

if(@valid_data.headers.present?)
  signature = OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha1'),
          S3_SECRET,
          @valid_data.headers).gsub(/\n/, '')
else

 signature = Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::Digest.new('sha1'),
      S3_SECRET,
      Base64.encode64(@valid_data.headers)
   )
  ).gsub(/\n/, '')

  details.policy = Base64.encode64({  expiration: @valid_data.expiration,
                                  conditions: @valid_data.conditions
                                }.to_json
                              ).gsub(/\n|\r/, '')
end

details.signature = signature

render :json => details, :status => 200 and return

但无法获得我所做的符号与亚马逊给出的符号匹配,因此我在每次上传时都会收到错误

我检查了github repo,rails s3帮助只指向https://github.com/Widen/fine-uploader-server/blob/master/rails.md,它指向https://github.com/Widen/fine-uploader-server/wiki/Rails---CarrierWave,它似乎与FineUploader没有任何关联。 最好, 温尼

编辑(澄清)

对于5mb文件,使用http://aws.amazon.com/articles/1434/#signyours3postform

中的代码对标题params进行签名可以正常工作
 signature = Base64.encode64(
    OpenSSL::HMAC.digest(
        OpenSSL::Digest::Digest.new('sha1'),
        S3_SECRET, @valid_data.headers)
    ).gsub("\n","")

对于子5MB文件,它需要策略和签名 FineUploader给了我一个"条件"参数,我用它来获取所有需要的信息:

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"]},
      {"content-length-range" => [content_length_range[1], content_length_range[2]]}
    ]

   policy = Base64.encode64({  "expiration" => @valid_data.expiration.to_s,
                                      "conditions" => conds
                                    }.to_json).
                          gsub("\n","")

    signature = Base64.encode64(
    OpenSSL::HMAC.digest(
        OpenSSL::Digest::Digest.new('sha1'),
        S3_SECRET, policy)
    ).gsub("\n","")

AWS始终以"无效的政策:无效的简单条件:价值mu .."在100%上升到aws之后。我似乎也无法在https://github.com/aws/aws-sdk-ruby

中找到政策签署功能

另外,根据ruby aws-sdk(https://github.com/aws/aws-sdk-ruby/blob/master/lib%2Faws%2Fcore%2Fsigners%2Fbase.rb),ruby使用sha256而不是sha1

2 个答案:

答案 0 :(得分:2)

您的问题对我来说不是很清楚,但如果您尝试直接将文件上传到 S3 ,则可以通过这种方式签署请求(取自s3_direct_upload gem)。我在我的应用程序中使用此gem将文件直接上传到 S3

    def policy
      Base64.encode64(policy_data.to_json).gsub("\n", "")
    end

    def policy_data
      {
        expiration: @options[:expiration],
        conditions: [
          ["starts-with", "$utf8", ""],
          ["starts-with", "$key", @options[:key_starts_with]],
          ["starts-with", "$x-requested-with", ""],
          ["content-length-range", 0, @options[:max_file_size]],
          ["starts-with","$content-type", @options[:content_type_starts_with] ||""],
          {bucket: @options[:bucket]},
          {acl: @options[:acl]},
          {success_action_status: "201"}
        ] + (@options[:conditions] || [])
      }
    end

    def signature
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest.new('sha1'),
          @options[:aws_secret_access_key], policy
        )
      ).gsub("\n", "")
    end

您可以在此处查看完整代码:https://github.com/waynehoover/s3_direct_upload/blob/master/lib/s3_direct_upload/form_helper.rb

答案 1 :(得分:1)

当我停止将内容长度范围传递给conds时,签名按预期工作