Skip to content

Instantly share code, notes, and snippets.

@steelx
Created May 13, 2018 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steelx/ba31b18f090faa10a6a135804539b80f to your computer and use it in GitHub Desktop.
Save steelx/ba31b18f090faa10a6a135804539b80f to your computer and use it in GitHub Desktop.
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