基于浏览器的上传使用POST [英] Browser-Based Uploads Using POST

查看:161
本文介绍了基于浏览器的上传使用POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用POST与AWS签名版本4的客户端上传。 按文件

I'm trying to create a client side uploads using POST with Signature Version 4 of AWS. According to the documents

当我生成的签名在服务器端,我得到在的这个例子页面。

When i'm generating the signature on the server side I get an exact match with AWS signature mentioned in this example page.

然而,当我用它来上传页面我得到这个错误:

However when I use it to upload the page I get this error:

我们计算并不您提供的签名相匹配SignatureDoesNotMatchThe请求签名。检查你的密钥和签名方法

SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method

这是在code我用:

OpenSSL::HMAC.hexdigest('sha256', signing_key(string_to_sign), string_to_sign)  

# step 2 in the aws documentation
def signing_key(encoded_policy)
  # generate the correct date
  date = extract_encoded_policy_date(encoded_policy)
  date = time_adjust(date)

  # encode all the fields by the algorithm
  date_key = OpenSSL::HMAC.digest('sha256',"AWS4#{@secret_access_key}", date.strftime("%Y%m%d"))
  date_region_key = OpenSSL::HMAC.digest('sha256',date_key, @region)
  date_region_service_key = OpenSSL::HMAC.digest('sha256',date_region_key, @service)
  signing_key = OpenSSL::HMAC.digest('sha256',date_region_service_key, 'aws4_request')

  signing_key
end

def time_adjust(date)
  time = Time.parse(date)
  time += time.utc_offset
  time.utc
end

在净一点点的搜索,我以前遇到过这种文章。 Iv'e实现了这个code和上载成功。

After a little search in the net, i've encountered this article. Iv'e implemented this code and the upload succeeded.

signature = OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), @secret_access_key, string_to_sign)
Base64.encode64(signature).gsub("\n","")```

这是一个小的演示为客户端code。

This is a small Demo for the client side code.

下面是一些文献,我发现有用:
一般说明
<一href="http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-ruby"相对=nofollow>从AWS一些code段

here is some literature I've found useful:
General description
Some code snippets from AWS

什么是两者之间的区别是什么?
我怎样才能得到工作,并上传我的文件的第一个选项? 在AWS页的例子不再有效?

What is the differences between the two?
How can I get the first option to work and upload my files? Is the example in the AWS page no longer valid?

推荐答案

研究和比较的 AWS张贴例如我发现有在做AWS认为我使用SHA1形式的一些冗余字段。

After a research and comparing the AWS post example i found out that there were some redundant fields in the form that made AWS think i'm using SHA1.

在从形式去除AWSAccessKeyId领域和重命名其他一些领域我设法使AWS4工作。

After removing the AWSAccessKeyId field from the form and renaming some other fields I managed to make the AWS4 work.

这是更新的演示

 <form id="myForm" action="http://yourbucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
      <input type="hidden" id="key" name="key" value="uploads/${filename}"/>
      <input type="hidden" id="acl" name="acl" value="YOUR_ACL_OPTION"/>
      <input type="hidden" name="success_action_redirect" value="http://google.com" />

      <input type="hidden" id="type" name="Content-Type" value="MIME_TYPE"/>
      <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
      <input type="hidden"   name="X-Amz-Credential" value="YOUR_CREDENTIALS" />
      <input type="hidden"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
      <input type="hidden" id="date" name="X-Amz-Date" value="" />

      <input type="hidden"  name="x-amz-meta-tag" value="" />
      <input type="hidden" id="policy" name="Policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED"/>
      <input type="hidden" id="signature" name="X-Amz-Signature" value="YOUR_CALCULATED_SIGNATURE"/>

      <input name="file" id="file" type="file"/> 
      <input id="btn_submit" class="btn btn-warning" type="submit" value="Upload File to S3"> 
 </form>

这篇关于基于浏览器的上传使用POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆