AWS s3 api错误:指定的存储桶不存在

时间:2014-10-21 16:47:18

标签: node.js amazon-web-services amazon-s3

我正在使用节点AWS SDK将图像保存到s3。尽管存在存储桶且我拥有正确的权限,但我仍然收到以下错误:

{ [NoSuchBucket: The specified bucket does not exist]
  message: 'The specified bucket does not exist',
  code: 'NoSuchBucket',
  time: Tue Oct 21 2014 12:32:50 GMT-0400 (EDT),
  statusCode: 404,
  retryable: false }

我的nodejs代码:

var fs = require('fs');

var AWS = require('aws-sdk'); //AWS library (used to provide temp credectials to a front end user)
AWS.config.loadFromPath('./AWS_credentials.json'); //load aws credentials from the     authentication text file



var s3 = new AWS.S3();

fs.readFile(__dirname + '/image.jpg', function(err, data) {

var params = {
    Bucket: 'https://s3.amazonaws.com/siv.io',
    Key: 'something',
};

s3.putObject(params, function(err, data) {

    if (err) {

        console.log(err);

    } else {
        console.log("Successfully uploaded data to myBucket/myKey");
    }
});
});

我还尝试了siv.io.s3-website-us-east-1.amazonaws.com作为存储桶名称。有人能让我知道我的错吗?如有必要,我可以提供更多信息。

1 个答案:

答案 0 :(得分:7)

错误表明该存在桶尚不存在。根据代码的外观,存储桶名称不正确,这就是无法找到文件的原因。拨打电话createBucket()或在您的AWS控制台中创建存储桶。

您也可以添加文件,而不仅仅是进行API调用。检查AWS API docs的放置位置。他们的文档非常好。

这就是我的所作所为:

    var stream = fs.createReadStream( 'path/to/file' );
    stream.on( 'error', function( error ) {
      seriesCb( error );
    } );
    //TODO: Other useful options here would be MD5 hash in the `ContentMD5` field,
    s3.putObject( {
      "Bucket": 'siv.io',
      "Key": 'name_of/new_file',
      "ContentType": "application/pdf", //might not apply to you
      "Body": stream
    }, function( s3err, s3results ) {
      if ( s3err ) return console.log('Bad stuff. ' + s3err.toString() );
      console.log( "Saved to S3. uri:" + s3uri);
    } );
相关问题