I am no JavaScript expert, and I have managed to hack together a node.js script that does exactly what I want it to do: upload images contained within a folder to Google Cloud Storage for a Firebase project, and then return the public access URLs. Given that I have limited experience with node.js combined with GC Storage, please advise me on any issues that running this code over approximately 200 images in the folder may cause.
var fs = require('fs');
const {Storage} = require('@google-cloud/storage');
const projectId = 'XXXXXXX';
//Creates a client
const storage = new Storage({
projectId: projectId,
keyFilename: 'auth.json'
});
// Reference the bucket
var bucket = storage.bucket('XXXXXXX.appspot.com');
//This reads the folder where the images are stored
fs.readdir('ImagesToUpload', (err, files) => {
if( err ) {
console.error( "Could not read the directory.", err );
process.exit( 1 );
}
files.forEach(function( file, index ) {
var filePath = 'ImagesToUpload/'
console.log(file)
// Upload a local file to a new file to be created in the bucket
bucket.upload(filePath += file, (err, file) => {
if (err) { return console.error(err); }
let publicUrl = `https://firebasestorage.googleapis.com/v0/b/${projectId}.appspot.com/o/${file.metadata.name}?alt=media`;
console.log(publicUrl);
});
})
})