6

My node.js application needs to upload files to S3. Most of the time, it will upload the file to an existing bucket. But sometimes, the bucket will need to be created first. Is there a way to check whether the bucket already exists, and if not, create it before initiating the upload? Here's what I've got so far:

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();
    }
  });

}
Dakota Lynch
  • 169
  • 2
  • 11
  • related https://stackoverflow.com/questions/50842835/how-do-i-test-if-a-bucket-exists-on-aws-s3 – Guido Mar 08 '19 at 22:17

2 Answers2

9

waitFor should be able to get you whether if a bucket does not exist.

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
});

If a bucket does not exist, then you can create it.

Hope it helps.

Guido
  • 46,642
  • 28
  • 120
  • 174
Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
0

You can put the create_bucket in a try catch block in python or you can list_buckets and search for a match.

import boto3

s3 = boto3.client('s3', region_name='us-east-1', 
                    # Set up AWS credentials 
                    aws_access_key_id=AWS_KEY_ID, 
                     aws_secret_access_key=AWS_SECRET)

buckets = s3.list_buckets()
buckets = [bucket['Name'] for bucket in response['Buckets'] if bucket['Name']==bucket_name]
if len(buckets)==0:
   s3_client.create_bucket(Bucket=bucket_name)
Golden Lion
  • 3,840
  • 2
  • 26
  • 35