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);
});
})
})
1 Answer 1
Looks good. A few nits:
You might as well use
const
where you can, andlet
when that doesn't work. You can avoidvar
completely.Instead of
var filePath = 'ImagesToUpload/'
-- and then modifyingfilePath
with+=
, just setfilePath
to the correct path immediately, ie.const filePath = 'ImagesToUpload' + file
. This is just simpler and easier to read.A subtler point: The logs are providing you your only reference to the file mapping onto the server. I'm concerned that these async functions won't necessarily run in order. Consider a huge file followed by a small file. Would the loop of files allow
bucket.upload
calls to be running at the same time? I would assume so. The small one would complete before the large one. If this happens, it's possible that your logconsole.log(file)
will happen out of sequence with theconsole.log(publicUrl)
. This can be solved by moving the first consolelog
into the loop right before thepublicUrl
log.Unused arguments at the end of the list can be omitted safely in JS, so
function( file, index )
can be simplyfunction( file)
, or evenfile =>
if you use the new ES syntax (which you are)
Explore related questions
See similar questions with these tags.