Skip to content

Instantly share code, notes, and snippets.

@porfidev
Created December 7, 2022 22:55
Show Gist options
  • Save porfidev/ca01c9619f75fc59ccf60df544422063 to your computer and use it in GitHub Desktop.
Save porfidev/ca01c9619f75fc59ccf60df544422063 to your computer and use it in GitHub Desktop.
How to download files from Firebase Storage
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Download Files from Storage</title>
</head>
<body>
<img id="myImage" alt="imagen prueba"/>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.14.0/firebase-app.js';
import {
getStorage,
ref,
getDownloadURL
} from 'https://www.gstatic.com/firebasejs/9.14.0/firebase-storage.js';
// Your web app's Firebase configuration
const firebaseConfig = await fetch('./firebaseConfig.json')
.then((response) => response.json());
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Cloud Storage and get a reference to the service
const storage = getStorage(app);
try {
const imageRef = ref(storage, 'imagenes/unnamed.jpeg');
const url = await getDownloadURL(imageRef);
const img = document.getElementById('myImage');
img.setAttribute('src', url);
/** Ejemplo con Fetch Request
// fetch(url).then(function (response) {
// return response.blob();
// }).then(function (myBlob) {
// console.log('url', url);
// console.log('imagen', myBlob);
// const objectURL = URL.createObjectURL(myBlob);
// img.src = objectURL;
// });
*/
} catch (error) {
console.log('error', error.code);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment