Skip to content

Commit 798d73f

Browse files
committed
Generate storage snippets
1 parent 282e711 commit 798d73f

28 files changed

+670
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/create-reference.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_create_ref_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
// Get a reference to the storage service, which is used to create references in your storage bucket
10+
const storage = getStorage(firebaseApp);
11+
12+
// Create a storage reference from our storage service
13+
const storageRef = ref(storage);
14+
// [END storage_create_ref_modular]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/create-reference.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_create_ref_child_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
11+
// Create a child reference
12+
const imagesRef = ref(storage, 'images');
13+
// imagesRef now points to 'images'
14+
15+
// Child references can also take paths delimited by '/'
16+
const spaceRef = ref(storage, 'images/space.jpg');
17+
// spaceRef now points to "images/space.jpg"
18+
// imagesRef still points to "images"
19+
// [END storage_create_ref_child_modular]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/create-reference.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_navigate_ref_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
const spaceRef = ref(storage, 'images/space.jpg');
11+
12+
// Parent allows us to move to the parent of a reference
13+
const imagesRef = spaceRef.parent;
14+
// imagesRef now points to 'images'
15+
16+
// Root allows us to move all the way back to the top of our bucket
17+
const rootRef = spaceRef.root;
18+
// rootRef now points to the root
19+
// [END storage_navigate_ref_modular]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/create-reference.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_navigate_ref_chain_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
const spaceRef = ref(storage, 'images/space.jpg');
11+
12+
// References can be chained together multiple times
13+
const earthRef = ref(spaceRef.parent, 'earth.jpg');
14+
// earthRef points to 'images/earth.jpg'
15+
16+
// nullRef is null, since the parent of root is null
17+
const nullRef = spaceRef.root.parent;
18+
// [END storage_navigate_ref_chain_modular]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/create-reference.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_ref_full_example_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
11+
// Points to the root reference
12+
const storageRef = ref(storage);
13+
14+
// Points to 'images'
15+
const imagesRef = ref(storageRef, 'images');
16+
17+
// Points to 'images/space.jpg'
18+
// Note that you can use variables to create child values
19+
const fileName = 'space.jpg';
20+
const spaceRef = ref(imagesRef, fileName);
21+
22+
// File path is 'images/space.jpg'
23+
const path = spaceRef.fullPath;
24+
25+
// File name is 'space.jpg'
26+
const name = spaceRef.name;
27+
28+
// Points to 'images'
29+
const imagesRefAgain = spaceRef.parent;
30+
// [END storage_ref_full_example_modular]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/create-reference.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_ref_properties_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
const spaceRef = ref(storage, 'images/space.jpg');
11+
12+
// Reference's path is: 'images/space.jpg'
13+
// This is analogous to a file path on disk
14+
spaceRef.fullPath;
15+
16+
// Reference's name is the last segment of the full path: 'space.jpg'
17+
// This is analogous to the file name
18+
spaceRef.name;
19+
20+
// Reference's bucket is the name of the storage bucket where files are stored
21+
spaceRef.bucket;
22+
// [END storage_ref_properties_modular]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/delete-files.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_delete_file_modular]
7+
import { getStorage, ref, deleteObject } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
11+
// Create a reference to the file to delete
12+
const desertRef = ref(storage, 'images/desert.jpg');
13+
14+
// Delete the file
15+
deleteObject(desertRef).then(() => {
16+
// File deleted successfully
17+
}).catch((error) => {
18+
// Uh-oh, an error occurred!
19+
});
20+
// [END storage_delete_file_modular]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/download-files.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_download_create_ref_modular]
7+
import { getStorage, ref } from "firebase/storage";
8+
9+
// Create a reference with an initial file path and name
10+
const storage = getStorage(firebaseApp);
11+
const pathReference = ref(storage, 'images/stars.jpg');
12+
13+
// Create a reference from a Google Cloud Storage URI
14+
const gsReference = ref(storage, 'gs://bucket/images/stars.jpg');
15+
16+
// Create a reference from an HTTPS URL
17+
// Note that in the URL, characters are URL escaped!
18+
const httpsReference = ref(storage, 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
19+
// [END storage_download_create_ref_modular]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/download-files.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_download_full_example_modular]
7+
import { getStorage, ref, getDownloadURL } from "firebase/storage";
8+
9+
// Create a reference to the file we want to download
10+
const storage = getStorage(firebaseApp);
11+
const starsRef = ref(storage, 'images/stars.jpg');
12+
13+
// Get the download URL
14+
getDownloadURL(starsRef)
15+
.then((url) => {
16+
// Insert url into an <img> tag to "download"
17+
})
18+
.catch((error) => {
19+
// A full list of error codes is available at
20+
// https://firebase.google.com/docs/storage/web/handle-errors
21+
switch (error.code) {
22+
case 'storage/object-not-found':
23+
// File doesn't exist
24+
break;
25+
case 'storage/unauthorized':
26+
// User doesn't have permission to access the object
27+
break;
28+
case 'storage/canceled':
29+
// User canceled the upload
30+
break;
31+
32+
// ...
33+
34+
case 'storage/unknown':
35+
// Unknown error occurred, inspect the server response
36+
break;
37+
}
38+
});
39+
// [END storage_download_full_example_modular]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./storage-next/download-files.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START storage_download_via_url_modular]
7+
import { getStorage, ref, getDownloadURL } from "firebase/storage";
8+
9+
const storage = getStorage(firebaseApp);
10+
getDownloadURL(ref(storage, 'images/stars.jpg'))
11+
.then((url) => {
12+
// `url` is the download URL for 'images/stars.jpg'
13+
14+
// This can be downloaded directly:
15+
const xhr = new XMLHttpRequest();
16+
xhr.responseType = 'blob';
17+
xhr.onload = (event) => {
18+
const blob = xhr.response;
19+
};
20+
xhr.open('GET', url);
21+
xhr.send();
22+
23+
// Or inserted into an <img> element
24+
const img = document.getElementById('myimg');
25+
img.setAttribute('src', url);
26+
})
27+
.catch((error) => {
28+
// Handle any errors
29+
});
30+
// [END storage_download_via_url_modular]

0 commit comments

Comments
 (0)