|
1 | 1 | # Firebase Storage
|
2 | 2 |
|
3 |
| -WIP |
| 3 | +[Firebase Storage](https://firebase.google.com/docs/storage/web/start) is a cloud storage service for Firebase. It allows you to store and serve user-generated content like images, audio, video, and other files. While most of the APIs can be used as you would normally do with Firebase, VueFire exposes a few composables to integrate better with Vue that not only give you access to reactive variables but also to some actions like `upload()` to update a file while keeping the reactive variable up to date at the same time. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You don't need to install anything specific to use Firebase Storage with VueFire. Just import the different composables you need from `vuefire` and you're good to go. |
| 8 | + |
| 9 | +You can access the Firebase Storage from within any component with the composable `useFirebaseStorage()`. |
| 10 | + |
| 11 | +## Uploading Files |
| 12 | + |
| 13 | +You can upload and monitor the progress of a file upload with the `useStorageFile()` composable. This also exposes the URL of the file once it's uploaded and it's metadata, let's start with a full example of a form upload: |
| 14 | + |
| 15 | +```vue |
| 16 | +<script setup lang="ts"> |
| 17 | +import { useFileDialog } from '@vueuse/core' |
| 18 | +import { ref as storageRef } from 'firebase/storage' |
| 19 | +import { useFirebaseStorage, useStorageFile } from 'vuefire' |
| 20 | +
|
| 21 | +const storage = useFirebaseStorage() |
| 22 | +const mountainFileRef = storageRef(storage, 'images/mountains.jpg') |
| 23 | +
|
| 24 | +const { |
| 25 | + url, |
| 26 | + // gives you a 0-1 value of the upload progress |
| 27 | + uploadProgress, |
| 28 | + uploadError, |
| 29 | + // firebase upload task |
| 30 | + uploadTask, |
| 31 | + upload, |
| 32 | +} = useStorageFile(mountainFileRef) |
| 33 | +
|
| 34 | +function uploadPicture() { |
| 35 | + const data = files.value?.item(0) |
| 36 | + if (data) { |
| 37 | + upload(data) |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +const filename = ref<string>() |
| 42 | +const { files, open, reset } = useFileDialog() |
| 43 | +</script> |
| 44 | +
|
| 45 | +<template> |
| 46 | + <form @submit.prevent="uploadPicture"> |
| 47 | + <!-- disable the form while uploading --> |
| 48 | + <fieldset :disabled="!!uploadTask"> |
| 49 | + <button |
| 50 | + type="button" |
| 51 | + @click="open({ accept: 'image/*', multiple: false })" |
| 52 | + > |
| 53 | + <template v-if="files?.length"> |
| 54 | + Selected file: {{ files.item(0)!.name }} (Click to select another) |
| 55 | + </template> |
| 56 | + <template v-else> Select one picture </template> |
| 57 | + </button> |
| 58 | +
|
| 59 | + <br /> |
| 60 | +
|
| 61 | + <button>Upload</button> |
| 62 | + </fieldset> |
| 63 | + </form> |
| 64 | +</template> |
| 65 | +``` |
| 66 | + |
| 67 | +Once the picture is uploaded, you can use the `url` reactive variable. For example, if it's an image, you can display it: |
| 68 | + |
| 69 | +```vue-html |
| 70 | +<img :src="url" /> |
| 71 | +``` |
| 72 | + |
| 73 | +## Downloading Files |
| 74 | + |
| 75 | +VueFire also exposes a smaller composable that only retrieves the url of a file. This is useful if you don't need to upload a file but only display it: |
| 76 | + |
| 77 | +```vue |
| 78 | +<script setup lang="ts"> |
| 79 | +import { useFileDialog } from '@vueuse/core' |
| 80 | +import { ref as storageRef } from 'firebase/storage' |
| 81 | +import { useFirebaseStorage, useStorageFile } from 'vuefire' |
| 82 | +
|
| 83 | +const storage = useFirebaseStorage() |
| 84 | +const mountainFileRef = storageRef(storage, 'images/mountains.jpg') |
| 85 | +const { |
| 86 | + url, |
| 87 | + // refresh the url if the file changes |
| 88 | + refresh, |
| 89 | +} = useStorageFileUrl(mountainFileRef) |
| 90 | +</script> |
| 91 | +``` |
| 92 | + |
| 93 | +## File metadata |
| 94 | + |
| 95 | +The same way you can access the file URL you can also access the file metadata. You can also use the `update()` function to update the metadata and keep the reactive variable up to date: |
| 96 | + |
| 97 | +```vue |
| 98 | +<script setup lang="ts"> |
| 99 | +import { useFileDialog } from '@vueuse/core' |
| 100 | +import { ref as storageRef } from 'firebase/storage' |
| 101 | +import { useFirebaseStorage, useStorageFile } from 'vuefire' |
| 102 | +
|
| 103 | +const storage = useFirebaseStorage() |
| 104 | +const mountainFileRef = storageRef(storage, 'images/mountains.jpg') |
| 105 | +const { |
| 106 | + metadata, |
| 107 | + // refresh the url if the file changes |
| 108 | + refresh, |
| 109 | + // update metadata |
| 110 | + update, |
| 111 | +} = useStorageFileUrl(mountainFileRef) |
| 112 | +</script> |
| 113 | +``` |
| 114 | + |
| 115 | +Note the metadata is accessible on the `useStorageFile()` composable as well. |
0 commit comments