我正在尝试使用来自heroku的预签名请求上传文件。
这是我从heroku回来的预先签名的请求:
{'url': u'https://bucketname.s3.amazonaws.com/track134', 'track_id': 70,
'x-amz-date': '20160928T162643Z', 'data': {'fields': {'acl': 'public-read',
'signature': u'M9k2Ke9GsExvxvvhB17n6Y1uIiM=', 'policy': u'eyJleHBpcmF0aW9uIjogIjIwMTYtMDktMjhUMTc6MjY6NDNaIiwgImNvbmRpdGlvbnMiOiBbeyJhY2wiOiAicHVibGljLXJlYWQifSwgeyJDb250ZW50LVR5cGUiOiAiYXVkaW8vbXA0In0sIHsiYnVje9N0IjogImZ1dKMyZWZpbGVzIn0sIHsia2Z2IjogInRyYWNrMTM0In1dfQ==',
'Content-Type': 'audio/mp4', 'AWSAccessKeyId': u'XXXXXXXXXXXX',
'key': u'track134'}, 'url': u'https://bucketname.s3.amazonaws.com/'}}
使用上面的值,我创建了这个多部分表单上传:
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(key.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "key") // "track134"
multipartFormData.append(acl.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "acl") // "public-read"
multipartFormData.append(contentType.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "Content-Type") // "audio/mp4"
multipartFormData.append(uuid.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "x-amz-meta-uuid") //"14365123651274"
multipartFormData.append(encryption.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "x-amz-server-side-encryption") "AES256"
multipartFormData.append(xamzcredential.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "X-Amz-Credential") // "<AWSAccessKey>/20160928/us-east-1/s3/aws4_request"
multipartFormData.append(algorithm.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "X-Amz-Algorithm") // "AWS4-HMAC-SHA256"
multipartFormData.append(isoDate.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "X-Amz-Date") // "20160928T162643Z"
multipartFormData.append(policy.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "Policy") // "same as pre-signed request
multipartFormData.append(signature.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "X-Amz-Signature") // "same as pre-signed request
multipartFormData.append(self.savedURL!, withName: "file", fileName: key, mimeType: contentType) //key = "track134" contentType = "audio/mp4"
},
to: "https://bucketname.s3.amazonaws.com/",
encodingCompletion:{ encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseString { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
不幸的是,我收到403错误:
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we
calculated does not match the signature you provided. Check your key and signing
method.</Message><AWSAccessKeyId>XXXXXXXXXXXXXX</AWSAccessKeyId>
<StringToSign>eyJleHBpcmF0aW9uIjogIjIwMTYtMDktMjhUMTc6MjY6NDNaIiwgImNvbmRpdGlvbnMiOiBbeyJhY2wiOiAicHVibGljLXJlYWQifSwgeyJDb250ZW50LVR5cGUiOiAiYXVkaW8vbXA0In0sIHsiYnVja2V0IjogImZ1dHVyZWZpbGVzIn0sIHsia2V5IjogInRyYWNrMTM0In1dfQ==</StringToSign>
<SignatureProvided>M9k2Ke9GsExvxvvhB17n6Y1uIiM=</SignatureProvided><StringToSignBytes>
65 79 4a 6c 65 48 42 70 63 6d 46 30 61 57 39 75 49 6a 6f 67 49 6a 49 77 4d 54 59 74 4d 44 6b 74 4d 6a 68 55 4d 54 63 36 4d 6a 59 36 4e 44 4e 61 49 69 77 67 49 6d 4e 76 62 6d 52 70 64 47 6c 76 62 6e 4d 69 4f 69 42 62 65 79 4a 68 59 32 77 69 4f 69 41 69 63 48 56 69 62 47 6c 6a 4c 58 4a 6c 59 57 51 69 66 53 77 67 65 79 4a 44 62 32 35 30 5a 57 35 30 4c 56 52 35 63 47 55 69 4f 69 41 69 59 58 56 6b 61 57 38 76 62 58 41 30 49 6e 30 73 49 48 73 69 59 6e 56 6a 61 32 56 30 49 6a 6f 67 49 6d 5a 31 64 48 56 79 5a 57 5a 70 62 47 56 7a 49 6e 30 73 49 48 73 69 61 32 56 35 49 6a 6f 67 49 6e 52 79 59 57 4e 72 4d 54 4d 30 49 6e 31 64 66 51 3d
3d</StringToSignBytes><RequestId>D232380DCEF794BE</RequestId>
<HostId>ly+8glzvyGtlO81QOrqNak7nAZngYZbD0upuSHUX07uT2FIz7OyztmM8wCVRXEfF3baIBIxtBK4=</HostId></Error>
我一直在网上寻找解决这个问题的提示,但不能真正打击任何东西。我希望有人能说出某些东西。
夫妻注意事项:我自己派出了X-Amz-Date
和X-Amz-Credential
- 不确定这是不是它的假设是如何起作用的。另外,我正在上传一个m4a的音频文件,我读了Content-Type
audio/mp4
,我将key
设置为文件名。