简体   繁体   English

通过回形针向S3提供Content-MD5标头

[英]Providing Content-MD5 header through paperclip to S3

I'm using Paperclip to upload files directly to s3 for my rails web app. 我正在使用Paperclip将文件直接上传到我的rails web应用程序的s3。 I'm currently trying to exploit the md5 check integrated in amazon s3 to verify that the upload was carried on successfully. 我目前正在尝试利用亚马逊s3中集成的md5检查来验证上传是否成功进行。 Paperclip offers a s3_headers hash that you can populate with whatever fields you need. Paperclip提供了一个s3_headers哈希,您可以填充所需的任何字段。 Content-Type is automatically filled. Content-Type会自动填写。 Content-MD5 needs to be Base64 encoded so I provide it this way: Content-MD5需要进行Base64编码,所以我这样提供:

:s3_headers => {:content_md5 => Base64.strict_encode64(md5sum)},

I use strict_encode64 because encode64 adds an unnecessary trailing \\n. 我使用strict_encode64因为encode64添加了一个不必要的尾随\\ n。 With this setup I always receive an InvalidDigest error from aws-sdk, even though paperclip correctly shows the calculated header. 使用此设置,我总是从aws-sdk收到InvalidDigest错误,即使回形针正确显示计算的标题。 I also tried to use plain, unencoded md5sum, with similar results. 我也尝试使用普通的,未编码的md5sum,结果相似。

If md5sum is a string of hex digits, like the std output from the Linux application md5sum , try this: 如果md5sum是一个十六进制数字的字符串,就像Linux应用程序md5sum的std输出一样,试试这个:

:s3_headers => {:content_md5 => [[md5sum].pack("H*")].pack("m0") }

For example, from the rails console: 例如,从rails控制台:

> md5sum = "7d592a3129ab6a867cf6e2eb60f9ef83"
> [[md5sum].pack("H*")].pack("m0")
 => "fVkqMSmraoZ89uLrYPnvgw=="

Take the md5 of your source, convert each (character couple) from string to hex (2 bytes become 1 byte); 取源的md5,将每个(字符对)从字符串转换为十六进制(2个字节变为1个字节); then base64 encode and you will be fine. 然后base64编码,你会没事的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM