How can i retrieve all files which are stored in google cloud bucket. (ALL FILES)?
Thanks and Regards, Tapas
Here is a solution that I have tested using a Node.js 8 HTTP Cloud Function. You can use it for the reference and modify it according to your needs:
My index.js file:
exports.listFiles = async(req, res) => {
let bucketName = req.body.bucket || req.query.bucket
// Import the Google Cloud Storage library
const {Storage} = require('@google-cloud/storage');
// Initiate a Storage client
const storage = new Storage();
// List files in the bucket and store their name in the array called 'result'
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push(file.name);
});
// Send the result upon a successful request
res.status(200).send(result);
};
My package.json file:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"@google-cloud/storage": "^4.1.3"
}
}
It can be called via the URL provided for triggering the function, by appending that URL with /?bucket=<name-of-your-bucket>
:
https://<function-region>-<project-name>.cloudfunctions.net/<function-name>/?bucket=<name-of-the-bucket>
Alternatively, it can also be called using curl:
curl --data "bucket=<name-of-your-bucket>" https://<functions-region>-<project-name>.cloudfunctions.net/<function-name>
["test-file-1.txt","test-file-2.png","test-file-3.png","test-file-4.png"]
Here's how you can change the index.js file to get the URLs instead of the filenames:
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push("https://storage.cloud.google.com/" + bucketName + "/" + file.name);
});
The whole index.js file:
exports.listFiles = async(req, res) => {
let bucketName = req.body.bucket || req.query.bucket
// Import the Google Cloud Storage library
const {Storage} = require('@google-cloud/storage');
// Initiate a Storage client
const storage = new Storage();
// List files in the bucket and store their name in the array called 'result'
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push("https://storage.cloud.google.com/" + bucketName + "/" + file.name);
});
// Send the result upon a successful request
res.status(200).send(result);
};