去装配一个 AJAX 上传到 S3,但请求以SignatureDoesNotMatch
. 在仔细检查了访问密钥 ID 和秘密之后,我对这个问题感到茫然。我可以确认我确实也设置了 CORS 策略文件。
这是 AJAX 请求:
$.ajax({
url: 'https://my-bucket.s3.amazonaws.com/',
type: 'POST',
contentType: false,
data: formData,
success: function() {
self.unblockUI($('#body'));
},
error: function() {
self.unblockUI($('#body'));
},
cache: false,
processData: false
});
在 Scala 中执行我的策略编码/签名。这是代码:
val policy = """
|{"expiration": "2020-01-01T00:00:00Z",
|"conditions": [
|{"bucket": "my-bucket"},
|["starts-with", "$key", "uploads/"],
|{"acl": "public-read"},
|{"success_action_redirect": "http://example.com/"},
|["starts-with", "$Content-Type", ""],
|["starts-with","$Filename",""],
|["content-length-range", 0, 5242880]
|]
|}
""".stripMargin.stripLineEnd.replaceAll("\n", "").replaceAll("\r","")
val policyEncoded = (new BASE64Encoder()).encode(policy.getBytes("UTF-8")).stripLineEnd.replaceAll("\n", "").replaceAll("\r","")
val hmac = Mac.getInstance("HmacSHA1")
hmac.init(new SecretKeySpec(AWS_SECRET.getBytes("UTF-8"), "HmacSHA1"))
val policySignature = (new BASE64Encoder()).encode(hmac.doFinal(policy.getBytes("UTF-8"))).stripLineEnd.replaceAll("\n", "").replaceAll("\r","")
发送的表格如下:
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="key"
uploads/reward/img/example_02820838f08sd083k.jpeg
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="AWSAccessKeyId"
<redacted>
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="acl"
public-read
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="success_action_redirect"
http://example.com/
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="policy"
eyJleHBpcmF0aW9uIjogIjIwMjAtMDEtMDFUMDA6MDA6MDBaIiwiY29uZGl0aW9ucyI6IFsgeyJidWNrZXQiOiAicGxhdGZvcm0zLWNsaWVudC1pbWFnZXMifSwgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInVwbG9hZHMvIl0seyJhY2wiOiAicHVibGljLXJlYWQifSx7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly8za3Vkb3MuY29tLyJ9LFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICIiXSxbInN0YXJ0cy13aXRoIiwiJEZpbGVuYW1lIiwiIl0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDUyNDI4ODBdXX0gICAg
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="signature"
9jj1hW8pGpS32Ka4KA2R0MwYKTQ=
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="Content-Type"
image/jpeg
------WebKitFormBoundaryTNKMpdRiJxhC39QF
Content-Disposition: form-data; name="file"; filename="youtube_bg.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryTNKMpdRiJxhC39QF--
关于为什么失败的任何想法?我想我已经用尽了已知的可能性。谢谢!