<template>
<div>
<input type="file" id="dosya" @change="putDesign()" />
</div>
</template>
<script>
import { initializeApp } from "firebase/app";
import { getStorage, ref, uploadBytes, listAll } from "firebase/storage";
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
};
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
export default {
data() {
return {
fileName: "",
resim: "",
photos: [],
};
},
methods: {
mounted() {
// Create a reference under which you want to list
const listRef = ref(storage, "design/");
// 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.
console.log(folderRef);
});
res.items.forEach((itemRef) => {
// All the items under listRef.
this.photos.push(itemRef+ " ");
});
})
.catch((error) => {
// Uh-oh, an error occurred!
console.log(error);
});
},
};
</script>
I can add files to my Firebase Storage, but I cannot show them on my website. I can get Local Storage Location but I cannot get Access Token, so I cannot open or show photos. Is there a way to get the Access Token? I searched on Firebase Documentation, but couldn't find anything helpful.
You just need to get the downloadURL for each element in the folder. It could look like this:
import { initializeApp } from "firebase/app";
import { getStorage, ref, uploadBytes, listAll, getDownloadURL } from "firebase/storage";
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
};
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
export default {
data() {
return {
fileName: "",
resim: "",
photos: [],
};
},
methods: {
mounted() {
// Create a reference under which you want to list
const listRef = ref(storage, "design/");
// 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.
console.log(folderRef);
});
res.items.forEach((itemRef) => {
// All the items under listRef.
getDownloadURL(itemRef).then(downloadURL=>{
this.photos.push(downloadURL);
})
});
})
.catch((error) => {
// Uh-oh, an error occurred!
console.log(error);
});
},
};