[英]Can't upload images in nodejs using aws-sdk
I've tried using aws-sdk and knox and I get status code 301 trying to upload images. 我尝试过使用aws-sdk和knox,我得到状态代码301试图上传图像。 I get status code 301 and message - 'The bucket you are attempting to access must be addressed using the specified endpoint.
我得到状态码301和消息 - '您尝试访问的存储桶必须使用指定的端点进行寻址。 Please send all future requests to this endpoint.
请将以后的所有请求发送到此端点。 This works in php.
这适用于PHP。
AWS.config.loadFromPath(__dirname + '/config/config.json');
fs.readFile(source, function (err, data) {
var s3 = new AWS.S3();
s3.client.createBucket({Bucket: 'mystuff'}, function() {
var d = {
Bucket: 'mystuff',
Key: 'img/test.jpg',
Body: data,
ACL: 'public-read'
};
s3.client.putObject(d, function(err, res) {
if (err) {
console.log("Error uploading data: ", err);
callback(err);
} else {
console.log("Successfully uploaded data to myBucket/myKey");
callback(res);
}
});
});
});
I actually solved this problem. 我实际上解决了这个问题。 In your config you have to have a region, since my bucket was "US Standard", I left my region blank and it worked.
在你的配置中你必须有一个区域,因为我的桶是“美国标准”,我把我的区域留空并且它有效。 config.json - { "accessKeyId": "secretKey", "secretAccessKey": "secretAccessKey", "region": ""}
config.json - {“accessKeyId”:“secretKey”,“secretAccessKey”:“secretAccessKey”,“region”:“”}
go to s3 management console select one of your files and click on proporties - > look at the file link. 转到s3管理控制台选择一个文件并单击比例 - >查看文件链接。 US standard https://s3.amazonaws.com/yourbucket/ host in your console window yourbucket.s3.amazonaws.com/
控制台窗口中的美国标准https://s3.amazonaws.com/yourbucket/ host yourbucket.s3.amazonaws.com/
us-west-1 https://s3-us-west-1.amazonaws.com/yourbucket/ host in your console window yourbucket.s3-us-west-1.amazonaws.com/ us-west-1 https://s3-us-west-1.amazonaws.com/yourbucket/ host at console console yourbucket.s3-us-west-1.amazonaws.com/
Did you try .send()? 你试过.send()吗?
I can upload to S3 by below code. 我可以通过下面的代码上传到S3。
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/AWSRequest.html http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/AWSRequest.html
var s3object = {Bucket: 'mystuff', Key: name, Body : data['data']};
s3.client.putObject(s3object).done(function(resp){
console.log("Successfully uploaded data");
}).fail(function(resp){
console.log(resp);
}).send();
I have the same problem with the new SDK and solved it by setting the endpoint option explicitly. 我对新SDK有同样的问题,并通过显式设置端点选项解决了这个问题。
Reference : http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details 参考: http : //docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details
Snippet: 片段:
var AWS = require('aws-sdk');
var s3 = new AWS.S3({ endpoint :'https://s3-your-region-varies.amazonaws.com' }),
myBucket = 'your-bucket-name';
var params = {Bucket: myBucket, Key: 'myUpload', Body: "Test"};
s3.putObject(params, function(err, data) {
if (err) {
console.log(err)
} else {
console.log("Successfully uploaded data to "+myBucket+"/testKeyUpload");
}
});
Alternatively, you can solve this by setting the region in your config file and you just have to be precise of your region name. 或者,您可以通过在配置文件中设置区域来解决此问题,您只需要准确了解您的区域名称即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.