Skip to content

Instantly share code, notes, and snippets.

@steelx steelx/listFiles.js
Created May 13, 2018

Embed
What would you like to do?
firebase storage list files
// listFiles.js
const gcs = require('@google-cloud/storage')({keyFilename: 'service-account-key.json'});
module.exports = function listFiles(request, response) {
if(!request.body.uid) {
response.status(422).send({error: "Missing UID"});
}
console.log("request.body.uid", request.body.uid);
// Creates a client
const bucketName = '<HIDDEN PROJECT ID>';
const prefix = 'users/';
// const prefix = `users/${request.body.uid}`;
const delimiter = '/';
const options = {
prefix: prefix,
};
console.log("options: ", options);
// if (delimiter) {
// options.delimiter = delimiter;
// }
// Lists files in the bucket, filtered by a prefix
const bucket = gcs.bucket(bucketName);
console.log("bucket: ", bucket);
bucket
.getFiles(options)
.then(results => {
const files = results[0];
// console.log('Files:');
// files.forEach(file => {
// console.log(file.name);
// });
response.send(files);
})
.catch(err => {
console.error('ERROR:', err);
response.status(400).send(err);
});
}
// @ index.js
const functions = require('firebase-functions');
const listFiles = require('./list_files');
exports.listFiles = functions.https.onRequest(listFiles);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.