I have created an array of my gcloud bucket files and I'd like to know how retrieve the file name and others metadata in order to display them in my code to get card like that for each file there is ? Thanks for your help.
app.js
router.get('/', async(req, res) => {
let bucketName = 'bucket-name'
const storage = Storage();
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
files.forEach(file => {
result.push(file.name);
});
res.render('views/list.ejs');
});
Have a look at the Node.js Client Library reference (scroll a bit down until 'Another example' where you can see available metadata attributes).
You can get the objects' metadata the following way:
const [files] = await storage.bucket(bucketName).getFiles();
let result = [];
for (const file of files) {
const [metadata] = await file.getMetadata();
result.push(metadata);
};
The code above will store the objects' metadata (in dictionaries) in the array 'result'. Here is how you could access metadata of an object belonging to that array:
let testObjName = result[0].name;
let testObjSize = result[0].size;
console.log(`Object ${testObjName} of ${testObjSize} bytes`);
I.e. metadata.name
will give you the name of the object, while metadata.size
- its size.