我想获得建,允许用户直接上传文件到我的Amazon S3的桶,从NodeJS动力网站。这似乎是唯一的教程在那里,比 实际亚马逊的文档该 都非常过时了。
I'm trying to get an built that allows users to upload a file directly to my Amazon S3 bucket, from a NodeJS powered website. It seems the only tutorials out there, other than the actual amazon docs for this are all very out of date.
我一直在下面的 本教程 ,对于基本信息,但同样它的过时。它没有方法调用加密
正确的,因为它试图原始JavaScript对象传递给更新
方法,它抛出一个错误,因为它不是一个字符串或缓冲区。
I've been following this tutorial, for the basic info, but again it's out dated. It doesn't have the method calls to crypto
correct, as it tries to pass a raw JavaScript object to the update
method, which throws an error because it's not a string or buffer.
我也一直在寻找的源代码的 诺克斯NPM包 。它没有内置的POST支持 - 这我完全理解,因为它是做自检,一旦有合适的领域的浏览器。诺克斯确实出现了有正确的code签署一项政策,我已经尽力让我的code工作在此基础上... ...但同样无济于事。
I've also been looking at the source for the knox npm package. It doesn't have POST support built in - which I totally understand, because it's the browser doing the POST once it has the right fields. Knox does appear to have the right code to sign a policy, and I've tried to get my code working based on this... but again to no avail.
下面是我想出来的,为code。它产生一个base64连接codeD政策,它创建了一个签名...但它是错误的签名根据亚马逊,当我尝试做一个文件上传。
Here is what I've come up with, for code. It produces a base64 encoded policy, and it creates a signature... but it's the wrong signature according to Amazon, when I try to do a file upload.
var crypto = require("crypto");
var config = require("../../amazonConfig.json");
exports.createS3Policy = function(callback) {
var date = new Date();
var s3Policy = {
"expiration": "2014-12-01T12:00:00.000Z",
"conditions": [
{"acl": "public-read"},
["content-length-range", 0, 2147483648],
{"bucket": "signalleaf"},
["starts-with", "$Cache-Control", ""],
["starts-with", "$Content-Type", ""],
["starts-with", "$Content-Disposition", ""],
["starts-with", "$Content-Encoding", ""],
["starts-with", "$Expires", ""],
["starts-with", "$key", "/myfolder/"],
{"success_action_redirect": "http://example.com/uploadsuccess"},
]
};
var stringPolicy = JSON.stringify(s3Policy).toString("utf-8");
var buffer = Buffer(stringPolicy, "utf-8");
var encoded = buffer.toString("base64");
var signature = crypto.createHmac("sha1", config.secretKey)
.update(new Buffer(stringPolicy, "utf-8")).digest("base64");
var s3Credentials = {
s3PolicyBase64: encoded,
s3Signature: signature
};
GLOBAL.s3creds = s3Credentials;
callback(s3Credentials);
};
我明明做错了什么,在这里。但是,我不知道。任何人都可以帮忙鉴定一下我做错了吗?当我的问题是? 有没有人有一个工作教程如何生成正确的Amazon S3的政策,有特色,从NodeJS v0.10.x,对于一个POST到S3 REST API?
推荐答案
好吧,我终于想通了。玩随机猜谜游戏了很长一段时间后,我心想
Ok, I finally figured it out. After playing the random guessing game for a VERY long time, I thought to myself
也许我需要签署的base64 EN codeD政策 - 我
"maybe i need to sign the base64 encoded policy" - me
和 BAM 仅此而已。
我也重新排序条件匹配形式如何被发布,虽然我不知道这有差别。
I also re-ordered the conditions to match how the form is posting, though I'm not sure this makes a difference.
var crypto = require("crypto");
var config = require("../../amazonConfig.json");
exports.createS3Policy = function(contentType, callback) {
var date = new Date();
var s3Policy = {
"expiration": "2014-12-01T12:00:00.000Z", // hard coded for testing
"conditions": [
["starts-with", "$key", "somefolder/"],
{"bucket": "my-bucket-name"},
{"acl": "public-read"},
["starts-with", "$Content-Type", contentType],
{"success_action_redirect": "http://example.com/uploadsuccess"},
]
};
// stringify and encode the policy
var stringPolicy = JSON.stringify(s3Policy);
var base64Policy = Buffer(stringPolicy, "utf-8").toString("base64");
// sign the base64 encoded policy
var signature = crypto.createHmac("sha1", config.secretKey)
.update(new Buffer(base64Policy, "utf-8")).digest("base64");
// build the results object
var s3Credentials = {
s3Policy: base64Policy,
s3Signature: signature
};
// send it back
callback(s3Credentials);
};
希望这会帮助别人运行到同样的问题。
Hopefully this will help others that run in to the same problem.
相关推荐
最新文章