I am uploading the same file multiple times "File.txt". Sometimes it successfully uploads to S3 and sometimes it raises:
SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method
I don't know what to test to discover the problem. Is it really a policy + signature problem as the exception says? So why sometimes can I upload?
Before upgrading to Rails 4 and updating some libs the same configurations always worked. But the updates in this case not even affect a simple POST form...
The POST params request sent with a failed upload were:
key: attachments/ad6d5c8c-f9ae-48ab-a9ff-c1b199a91d1d/File.txt
acl: public-read
policy: XXX==
signature: YYY=
Content-Type: binary/octet-stream
AWSAccessKeyId: -- Hidden --
...And with a successful one:
key: attachments/368f5497-6e11-4f07-b379-80020e902013/File.txt
acl: public-read
policy: XXX==
signature: ZZZ=
Content-Type: binary/octet-stream
AWSAccessKeyId: -- Hidden --
It seems right just like before. The only attribute that changed was the signature because of the random guid generation in the path...
Here are some other methods for generation policies:
def fields
{
key: key,
acl: acl,
policy: policy,
signature: signature,
"Content-Type" => content_type || "binary/octet-stream",
"AWSAccessKeyId" => S3_CONFIG["access_key_id"]
}
end
def policy
Base64.encode64(policy_data.to_json).gsub("\n", "")
end
def policy_data
{
expiration: expiration,
conditions: [
{bucket: S3_CONFIG["bucket"]},
["starts-with", "$key", store_dir],
{acl: acl},
["starts-with", "$Content-Type", ''],
["content-length-range", min_file_size, max_file_size]
]
}
end
def signature
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
S3_CONFIG["secret_access_key"], policy
)
).gsub("\n", "")
end
Any help I appreciate Thanks