将文件上传到S3(frankfurt)会获得错误授权AWS4-HMAC-SHA256
Uploading a file to S3 (frankfurt) gets an error authorization AWS4-HMAC-SHA256
嗨:)当我试图直接将文件上传到AWS S3时,我会收到以下错误:
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.
似乎我在签署v4-hmac授权签名时遗漏了一些内容:''
html文件:
<form action="https://mybucket.s3.eu-central-1.amazonaws.com/" enctype="multipart/form-data" id="form-upload" method="POST">
<input type="hidden" name="AWSAccessKeyId" value="myPublicKey">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="http://localhost:8080/uploadAfter">
<input type="hidden" name="Content-type" value=""> <!-- any file format -->
<input type="hidden" name="x-amz-algorithm" value="AWS4-HMAC-SHA256">
<!-- The following are injected by Javascript -->
<input type="hidden" name="key">
<input type="hidden" name="policy">
<input type="hidden" name="signature">
<input type="hidden" name="x-amz-credential">
<input type="hidden" name="x-amz-date">
<input type="hidden" name="x-amz-signature">
<input type="file" id="inputFile">
<button id="form-upload-submit" type="submit">Upload</button>
</form>
服务器端节点文件("getS3Credentials ajax请求"):
var crypto = require("crypto"),
AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY,
AWS_SECRET_KEY = process.env.AWS_SECRET_KEY,
dateNow = new Date().toISOString(),
dateNowRaw = dateNow.substr(0, dateNow.indexOf("T")).replace(/-/g, ""),
expiration = new Date();
expiration.setHours(expiration.getHours() + 1);
expiration = expiration.toISOString();
var policy = {
expiration: expiration,
conditions: [
{bucket: "mybucket"},
["starts-with", "$key", "files/filename"],
{acl: "private"},
{success_action_redirect: "http://localhost:8080/uploadAfter"},
["starts-with", "$Content-Type", ""],
["content-length-range", 0, 1024*1024*100],
{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
{"x-amz-credential": AWS_ACCESS_KEY + "/" + dateNowRaw + "/eu-central-1/s3/aws4_request"},
{"x-amz-date": dateNow}
],
},
policyString = JSON.stringify(policy),
encodedPolicyString = new Buffer(policyString, "utf-8").toString("base64"),
dateKey = app.crypto
.createHmac("sha256", "AWS4" + AWS_SECRET_KEY)
.update(dateNowRaw)
.digest('binary'),
dateRegionKey = app.crypto
.createHmac("sha256", dateKey)
.update("eu-central-1")
.digest('binary'),
dateRegionServiceKey = app.crypto
.createHmac("sha256", dateRegionKey)
.update("s3")
.digest('binary'),
signinKey = app.crypto
.createHmac("sha256", dateRegionServiceKey)
.update("aws4_request")
.digest('binary'),
signature = app.crypto
.createHmac("sha256", signinKey)
.update(encodedPolicyString)
.digest('base64');
$.send({awskeyid: AWS_ACCESS_KEY, s3_policy: encodedPolicyString, s3_signature: signature, s3_key: "files/filename", s3_date_now: dateNow, s3_date_now_raw: dateNowRaw});
客户端js文件:
document.getElementById("form-upload-submit").onclick = function(evt) {
evt.preventDefault();
formUpload();
};
function formUpload() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/upload");
xhr.responseType = "json";
xhr.onload = processUpload;
xhr.send();
}
function processUpload() {
var response = this.response;
document.querySelector("#form-upload input[type='hidden'][name='key']").value = response.s3_key;
document.querySelector("#form-upload input[type='hidden'][name='policy']").value = response.s3_policy;
document.querySelector("#form-upload input[type='hidden'][name='signature']").value = response.s3_signature;
document.querySelector("#form-upload input[type='hidden'][name='x-amz-credential']").value = response.awskeyid + "/" + response.s3_date_now_raw + "/eu-central-1/s3/aws4_request";
document.querySelector("#form-upload input[type='hidden'][name='x-amz-date']").value = response.s3_date_now;
document.getElementById("form-upload").submit();
//The error is shown here as xml page, after the submitting of the form with the redirect to the amazon upload url
}
感谢您的帮助;)
您应该从表单中删除字段"AWSAccessKeyId"。
如果您将此字段添加到表单中,AWS将尝试使用SHA1验证签名,并忽略您提到的算法类型。
看看这个aws-post的例子,检查表单中的字段是否与aws表单匹配(你可以使用这个工具
另一个选项是使用SHA1散列
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- javascript:如何在antlr生成的Lexer中进行错误处理
- Angularjs工厂注入错误
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 将文件上传到S3(frankfurt)会获得错误授权AWS4-HMAC-SHA256