Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果不存在S3 Bucket,则创建S3 Bucket_Node.js_Amazon S3 - Fatal编程技术网

Node.js 如果不存在S3 Bucket,则创建S3 Bucket

Node.js 如果不存在S3 Bucket,则创建S3 Bucket,node.js,amazon-s3,Node.js,Amazon S3,我的node.js应用程序需要将文件上载到S3。大多数情况下,它会将文件上传到现有的bucket中。但有时,需要首先创建bucket。是否有办法检查bucket是否已经存在,如果不存在,在开始上传之前创建它?以下是到目前为止我得到的信息: function uploadit () { 'use strict'; console.log('Preparing to upload the verse.') var s3 = new AWS.S3({apiVersion: '2006-03

我的node.js应用程序需要将文件上载到S3。大多数情况下,它会将文件上传到现有的bucket中。但有时,需要首先创建bucket。是否有办法检查bucket是否已经存在,如果不存在,在开始上传之前创建它?以下是到目前为止我得到的信息:

function uploadit () {
  'use strict';
  console.log('Preparing to upload the verse.')
  var s3 = new AWS.S3({apiVersion: '2006-03-01'});
  var uploadParams = {Bucket: 'DynamicBucketName', key: '/test.mp3', Body: myvalue, ACL: 'public-read'};
  var file = 'MyVerse.mp3';

  var fs = require('fs');
  var fileStream = fs.createReadStream(file);
  fileStream.on('error', function(err) {
    console.log('File Error', err);
  });
  uploadParams.Body = fileStream;

  var path = require('path');
  uploadParams.Key = book.replace(/ /g, "")+reference.replace(/ /g, "")+"_user"+userid+".mp3";

  // call S3 to retrieve upload file to specified bucket
  s3.upload (uploadParams, function (err, data) {
    if (err) {
      console.log("Error", err);
    } if (data) {
      console.log("Upload Success", data.Location);
      linkit();
      addanother();
    }
  });

}
如果一个桶不存在,应该能够得到你

var params = {
  Bucket: 'STRING_VALUE' /* required */
};
s3.waitFor('bucketNotExists', params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
如果bucket不存在,则可以创建它

希望有帮助。

相关