-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathcreate-reference.js
112 lines (84 loc) · 3.19 KB
/
create-reference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
function createRef() {
// [START storage_create_ref]
const { getStorage, ref } = require("firebase/storage");
// Get a reference to the storage service, which is used to create references in your storage bucket
const storage = getStorage();
// Create a storage reference from our storage service
const storageRef = ref(storage);
// [END storage_create_ref]
}
function createRefChild() {
// [START storage_create_ref_child]
const { getStorage, ref } = require("firebase/storage");
const storage = getStorage();
// Create a child reference
const imagesRef = ref(storage, 'images');
// imagesRef now points to 'images'
// Child references can also take paths delimited by '/'
const spaceRef = ref(storage, 'images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"
// [END storage_create_ref_child]
}
function navigateRef() {
// [START storage_navigate_ref]
const { getStorage, ref } = require("firebase/storage");
const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');
// Parent allows us to move to the parent of a reference
const imagesRef = spaceRef.parent;
// imagesRef now points to 'images'
// Root allows us to move all the way back to the top of our bucket
const rootRef = spaceRef.root;
// rootRef now points to the root
// [END storage_navigate_ref]
}
function navigateRefChain() {
// [START storage_navigate_ref_chain]
const { getStorage, ref } = require("firebase/storage");
const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');
// References can be chained together multiple times
const earthRef = ref(spaceRef.parent, 'earth.jpg');
// earthRef points to 'images/earth.jpg'
// nullRef is null, since the parent of root is null
const nullRef = spaceRef.root.parent;
// [END storage_navigate_ref_chain]
}
function refProperties() {
// [START storage_ref_properties]
const { getStorage, ref } = require("firebase/storage");
const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');
// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;
// Reference's name is the last segment of the full path: 'space.jpg'
// This is analogous to the file name
spaceRef.name;
// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;
// [END storage_ref_properties]
}
function refFullExample() {
// [START storage_ref_full_example]
const { getStorage, ref } = require("firebase/storage");
const storage = getStorage();
// Points to the root reference
const storageRef = ref(storage);
// Points to 'images'
const imagesRef = ref(storageRef, 'images');
// Points to 'images/space.jpg'
// Note that you can use variables to create child values
const fileName = 'space.jpg';
const spaceRef = ref(imagesRef, fileName);
// File path is 'images/space.jpg'
const path = spaceRef.fullPath;
// File name is 'space.jpg'
const name = spaceRef.name;
// Points to 'images'
const imagesRefAgain = spaceRef.parent;
// [END storage_ref_full_example]
}