Reputation: 31
I have a folder in the storage bucket where I store photos. Suppose the limit of this folder is 1500 photos. when this limit is reached I want to delete 500 photos so now only 1000 photos are left. Is there any way to get total items in folder and then delete 500 oldest photos? or should I create reference in realtime database and store photos reference there. I am new to firebase and actually want to implement queue of photos in that folder.
Upvotes: 1
Views: 113
Reputation: 4293
Create a reference about the storage data and the count if possible Delete the photos once the count is 1500
Or use a cloud function that does this for you, if there is any way to get the storage info inside the cloud function. So that you don't want to manage anything on RTDB.
Upvotes: 0
Reputation:
You can listAll()
in a Reference
. Here is the Documentaion. Then you can iterate all elements with a each function and get lenght.
The code would be something like:
// Create a reference under which you want to list
var storageRef = firebase.storage().ref();
var listRef = storageRef.child('YOUR_FOLDER');
var items = 0;
// Find all the prefixes and items.
listRef
.listAll()
.then(function (res) {
res.prefixes.forEach(function (folderRef) {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
});
res.items.forEach(function (itemRef) {
// All the items under listRef.
items++;
});
console.log(items);
})
.catch(function (error) {
// Uh-oh, an error occurred!
});
In this example I create a items variable as a count, but you can push all elements in array and then get lenght().
Upvotes: 0
Reputation: 317828
Yes, you should use a database to store references to the files in the bucket. The database will be much easier for you to query using whatever data about the file you store along with it. The bucket does not have a good way to query for age.
Upvotes: 1