Created
May 13, 2018 14:05
-
-
Save steelx/ba31b18f090faa10a6a135804539b80f to your computer and use it in GitHub Desktop.
firebase storage list files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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