I have some images inside a directory in Google Cloud Storage. I am trying to list all the images inside the directory. The problem is that the files are not consistent in order. Sometimes it will be in order and sometimes not. I want the files in exact order that is displayed on the Google Cloud Platform. How can I do it?
Here is the reference code from documentation:
const storage = getStorage();
// Create a reference under which you want to list
const listRef = ref(storage, 'files/uid');
// Find all the prefixes and items.
listAll(listRef)
.then((res) => {
res.prefixes.forEach((folderRef) => {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
});
res.items.forEach((itemRef) => {
// All the items under listRef.
});
}).catch((error) => {
// Uh-oh, an error occurred!
});
This is my sample code
var storage = getStorage();
var listRef = ref(storage, "Data");
listAll(listRef)
.then((res) => {
res.prefixes.forEach((folderRef) => {});
res.items.forEach((itemRef) => {
// All the items under listRef.
var location = itemRef._location.path_;
console.log(location);
getDownloadURL(ref(storage, location))
.then((url) => {
console.log(url);
})
.catch((error) => {
console.log(error);
});
});
})
.catch((error) => {
// Uh-oh, an error occurred!
console.log(error);
});
I want the location and url of the files inside Storage to be in the same order as they are displayed in Storage inside Firebase. But when I reload, they are not consistent in their order. This is the order which I uploaded the files:
But when I console.log the url, often the order is not the same.