Node.js 如何正确地上传到亚马逊S3与流星弹弓?
我正在尝试上传到S3,我在web控制台中不断遇到的错误是 传递调用“slingshot/uploadRequest”的结果时出现异常:TypeError:>无法读取未定义的属性“response” 这是我的服务器端代码:Node.js 如何正确地上传到亚马逊S3与流星弹弓?,node.js,amazon-web-services,meteor,amazon-s3,meteor-slingshot,Node.js,Amazon Web Services,Meteor,Amazon S3,Meteor Slingshot,我正在尝试上传到S3,我在web控制台中不断遇到的错误是 传递调用“slingshot/uploadRequest”的结果时出现异常:TypeError:>无法读取未定义的属性“response” 这是我的服务器端代码: Slingshot.fileRestrictions("Test1", { allowedFileTypes: ["image/png", "image/jpeg", "image/gif"], maxSize: 10 * 1024 * 1024 // 10 MB (u
Slingshot.fileRestrictions("Test1", {
allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});
Slingshot.createDirective("Test1", Slingshot.S3Storage, {
AWSAccessKeyId: "Key",
AWSSecretAccessKey: "Key",
bucket: "bucketname",
acl: "public-read",
authorize: function () {
//Deny uploads if user is not logged in.
},
key: function (file) {
//Store file into a directory by the user's username.
return file.name;
}
客户端代码:
Template.first.events({
'change .fileInput': function(event, template) {
event.preventDefault();
var uploader = new Slingshot.Upload("Test1");
var docc = document.getElementById('fileup').files[0];
console.log(docc);
uploader.send(docc, function (error){
if (error) {
console.error('Error uploading', uploader.xhr.response);
alert (error);
}
else{
console.log("Worked!");
}
});
}
非常感谢您的帮助 在createDirective命令块中引用区域也很重要,例如
Slingshot.createDirective("Test1", Slingshot.S3Storage, {
bucket: "bucketname",
region: "eu-west-1"
...
如果没有明确指定区域,我得到的只是一个“400-Bad请求”
AWSAccessKeyId和AWSSecretAccessKey应该放在一个单独的settings.json文件中,该文件仅限于服务器的部署,并且从源代码管理(github)中排除
settings.json:
{
"AWSAccessKeyId": "<insert key id here>",
"AWSSecretAccessKey": "<insert access key here>"
}
{
“AWSAccessKeyId”:“,
“AWSSecretAccessKey”:”
}
通常,每个环境(dev、acpt、live)都有一个设置文件,这些文件都是同一代码库的一部分,并在运行时为所需的环境选择。行下失败。xhr为空
console.error('Error uploading', uploader.xhr.response);
如下面所示记录错误并从那里进行调试
console.error('Error uploading', error);
有两件事我花了一段时间才弄明白。1.你设置了区域吗?它默认为“us-east-1”。如果这不是你的,那么你需要告诉slingshot。2.S3配置。你的应用程序(即S3用户)有权访问你的存储桶吗?我将区域更改为“us-east-1”“,但它仍然给我同样的错误。我不知道你说的第二点是什么意思。我只是假设你在IAM管理控制台中创建了一个用户(你的应用程序)来为你的应用程序生成密钥。您可以在那里授予权限。是否存在有关错误的堆栈跟踪?如果是的话,你能把它寄出去吗?此外,不需要在服务器端创建文件限制,除非您与客户端共享该代码。你可以直接在指令中设置相同的属性。嗨,你发现问题出在哪里了吗?我也有同样的问题