Skip to content

Commit 131773d

Browse files
docs(storage): Add docs from firebase.google.com (#8248)
1 parent 8879519 commit 131773d

File tree

11 files changed

+1079
-528
lines changed

11 files changed

+1079
-528
lines changed

docs/sidebars.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ module.exports = {
140140
],
141141
"Cloud Storage": [
142142
"storage/overview",
143-
"storage/usage",
143+
"storage/start",
144+
"storage/create-reference",
145+
"storage/upload-files",
146+
"storage/download-files",
147+
"storage/file-metadata",
148+
"storage/delete-files",
149+
"storage/list-files",
150+
"storage/handle-errors",
144151
toReferenceAPI("firebase_storage"),
145152
toGithubExample("firebase_storage"),
146153
],

docs/storage/create-reference.mdx

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: Create a Cloud Storage reference on Flutter
3+
sidebar_label: Create a reference
4+
---
5+
6+
Your files are stored in a
7+
[Cloud Storage](//cloud.google.com/storage) bucket. The
8+
files in this bucket are presented in a hierarchical structure, just like the
9+
file system on your local hard disk, or the data in the Firebase Realtime Database.
10+
By creating a reference to a file, your app gains access to it. These references
11+
can then be used to upload or download data, get or update metadata or delete
12+
the file. A reference can either point to a specific file or to a higher level
13+
node in the hierarchy.
14+
15+
If you've used the [Firebase Realtime Database](/docs/database/overview), these paths should
16+
seem very familiar to you. However, your file data is stored in
17+
Cloud Storage, **not** in the Realtime Database.
18+
19+
20+
## Create a Reference
21+
22+
Create a reference to upload, download, or delete a file,
23+
or to get or update its metadata. A reference
24+
can be thought of as a pointer to a file in the cloud. References are
25+
lightweight, so you can create as many as you need. They are also reusable for
26+
multiple operations.
27+
28+
Create a reference using the `FirebaseStorage` singleton instance and
29+
calling its `ref()` method.
30+
31+
```dart
32+
final storageRef = FirebaseStorage.instance.ref();
33+
```
34+
35+
Next, you can create a reference to a location lower in the tree,
36+
say `"images/space.jpg"` by using the `child()` method on an existing reference.
37+
38+
```dart
39+
// Create a child reference
40+
// imagesRef now points to "images"
41+
final imagesRef = storageRef.child("images");
42+
43+
// Child references can also take paths
44+
// spaceRef now points to "images/space.jpg
45+
// imagesRef still points to "images"
46+
final spaceRef = storageRef.child("images/space.jpg");
47+
```
48+
49+
## Navigate with References
50+
51+
You can also use the `parent` and `root` properties to navigate up in our
52+
file hierarchy. `parent` navigates up one level,
53+
while `root` navigates all the way to the top.
54+
55+
```dart
56+
// parent allows us to move our reference to a parent node
57+
// imagesRef2 now points to 'images'
58+
final imagesRef2 = spaceRef.parent;
59+
60+
// root allows us to move all the way back to the top of our bucket
61+
// rootRef now points to the root
62+
final rootRef = spaceRef.root;
63+
```
64+
65+
`child()`, `parent`, and `root` can be chained together multiple
66+
times, as each is a reference. But accessing `root.parent` results in `null`.
67+
68+
```dart
69+
// References can be chained together multiple times
70+
// earthRef points to 'images/earth.jpg'
71+
final earthRef = spaceRef.parent?.child("earth.jpg");
72+
73+
// nullRef is null, since the parent of root is null
74+
final nullRef = spaceRef.root.parent;
75+
```
76+
77+
78+
## Reference Properties
79+
80+
You can inspect references to better understand the files they point to
81+
using the `fullPath`, `name`, and `bucket` properties. These properties
82+
get the file's full path, name and bucket.
83+
84+
```dart
85+
// Reference's path is: "images/space.jpg"
86+
// This is analogous to a file path on disk
87+
spaceRef.fullPath;
88+
89+
// Reference's name is the last segment of the full path: "space.jpg"
90+
// This is analogous to the file name
91+
spaceRef.name;
92+
93+
// Reference's bucket is the name of the storage bucket that the files are stored in
94+
spaceRef.bucket;
95+
```
96+
97+
## Limitations on References
98+
99+
Reference paths and names can contain any sequence of valid Unicode characters,
100+
but certain restrictions are imposed including:
101+
102+
1. Total length of reference.fullPath must be between 1 and 1024 bytes when UTF-8 encoded.
103+
1. No Carriage Return or Line Feed characters.
104+
1. Avoid using `#`, `[`, `]`, `*`, or `?`, as these do not work well with
105+
other tools such as the [Firebase Realtime Database](/docs/database/overview)
106+
or [gsutil](https://cloud.google.com/storage/docs/gsutil).
107+
108+
## Full Example
109+
110+
```dart
111+
// Points to the root reference
112+
final storageRef = FirebaseStorage.instance.ref();
113+
114+
// Points to "images"
115+
Reference? imagesRef = storageRef.child("images");
116+
117+
// Points to "images/space.jpg"
118+
// Note that you can use variables to create child values
119+
final fileName = "space.jpg";
120+
final spaceRef = imagesRef.child(fileName);
121+
122+
// File path is "images/space.jpg"
123+
final path = spaceRef.fullPath;
124+
125+
// File name is "space.jpg"
126+
final name = spaceRef.name;
127+
128+
// Points to "images"
129+
imagesRef = spaceRef.parent;
130+
```
131+
132+
Next, let's learn how to [upload files](upload-files) to Cloud Storage.

docs/storage/delete-files.mdx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Delete files with Cloud Storage on Flutter
3+
sidebar_label: Delete files
4+
---
5+
6+
After uploading files to Cloud Storage, you can also delete them.
7+
8+
:::note
9+
By default, a Cloud Storage bucket requires Firebase Authentication to
10+
perform any action on the bucket's data or files. You can
11+
[change your Firebase Security Rules for Cloud Storage](https://firebase.google.com/docs/storage/security/rules-conditions#public)
12+
to allow unauthenticated access. Since Firebase and your project's default
13+
App Engine app share this bucket, configuring public access may make newly
14+
uploaded App Engine files publicly accessible, as well. Be sure to restrict
15+
access to your Cloud Storage bucket again when you set up Authentication.
16+
:::
17+
18+
19+
## Delete a File
20+
21+
To delete a file, first [create a reference](create-reference)
22+
to that file. Then call the `delete()` method on that reference.
23+
24+
```dart
25+
// Create a reference to the file to delete
26+
final desertRef = storageRef.child("images/desert.jpg");
27+
28+
// Delete the file
29+
await desertRef.delete();
30+
```
31+
32+
:::note
33+
Deleting a file is a permanent action! If you care about restoring
34+
deleted files, make sure to back up your files, or
35+
[enable Object Versioning](https://cloud.google.com/storage/docs/using-object-versioning#set)
36+
on your Cloud Storage bucket.
37+
:::
38+
39+
## Handle Errors
40+
41+
There are a number of reasons why errors may occur on file deletes,
42+
including the file not existing, or the user not having permission
43+
to delete the desired file. More information on errors can be found in the
44+
[Handle Errors](handle-errors) section of the docs.

docs/storage/download-files.mdx

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
title: Download files with Cloud Storage on Flutter
3+
sidebar_label: Download files
4+
---
5+
6+
Cloud Storage for Firebase allows you to quickly and easily download
7+
files from a [Cloud Storage](//cloud.google.com/storage)
8+
bucket provided and managed by Firebase.
9+
10+
:::note
11+
By default, a Cloud Storage bucket requires Firebase Authentication to
12+
perform any action on the bucket's data or files. You can
13+
[change your Firebase Security Rules for Cloud Storage](https://firebase.google.com/docs/storage/security/rules-conditions#public)
14+
to allow unauthenticated access. Since Firebase and your project's default
15+
App Engine app share this bucket, configuring public access may make newly
16+
uploaded App Engine files publicly accessible, as well. Be sure to restrict
17+
access to your Cloud Storage bucket again when you set up Authentication.
18+
:::
19+
20+
21+
## Create a Reference
22+
23+
To download a file, first [create a Cloud Storage reference](create-reference)
24+
to the file you want to download.
25+
26+
You can create a reference by appending child paths to the root of your
27+
Cloud Storage bucket, or you can create a reference from an existing
28+
`gs://` or `https://` URL referencing an object in Cloud Storage.
29+
30+
```dart
31+
// Create a storage reference from our app
32+
final storageRef = FirebaseStorage.instance.ref();
33+
34+
// Create a reference with an initial file path and name
35+
final pathReference = storageRef.child("images/stars.jpg");
36+
37+
// Create a reference to a file from a Google Cloud Storage URI
38+
final gsReference =
39+
FirebaseStorage.instance.refFromURL("gs://YOUR_BUCKET/images/stars.jpg");
40+
41+
// Create a reference from an HTTPS URL
42+
// Note that in the URL, characters are URL escaped!
43+
final httpsReference = FirebaseStorage.instance.refFromURL(
44+
"https://firebasestorage.googleapis.com/b/YOUR_BUCKET/o/images%20stars.jpg");
45+
```
46+
47+
48+
## Download Files
49+
50+
Once you have a reference, you can download files from Cloud Storage
51+
by calling the `getData()` or `getStream()`. If you prefer to download the file
52+
with another library, you can get a download URL with `getDownloadUrl()`.
53+
54+
### Download in memory
55+
56+
Download the file to a `UInt8List` with the `getData()` method. This is the
57+
easiest way to download a file, but it must load the entire contents of
58+
your file into memory. If you request a file larger than your app's available
59+
memory, your app will crash. To protect against memory issues, `getData()`
60+
takes a maximum amount of bytes to download. Set the maximum size to something
61+
you know your app can handle, or use another download method.
62+
63+
```dart
64+
final islandRef = storageRef.child("images/island.jpg");
65+
66+
try {
67+
const oneMegabyte = 1024 * 1024;
68+
final Uint8List? data = await islandRef.getData(oneMegabyte);
69+
// Data for "images/island.jpg" is returned, use this as needed.
70+
} on FirebaseException catch (e) {
71+
// Handle any errors.
72+
}
73+
```
74+
75+
### Download to a local file
76+
77+
The `writeToFile()` method downloads a file directly to a local device. Use this if
78+
your users want to have access to the file while offline or to share the file in a
79+
different app. `writeToFile()` returns a `DownloadTask` which you can use to manage
80+
your download and monitor the status of the download.
81+
82+
```dart
83+
final islandRef = storageRef.child("images/island.jpg");
84+
85+
final appDocDir = await getApplicationDocumentsDirectory();
86+
final filePath = "${appDocDir.absolute}/images/island.jpg";
87+
final file = File(filePath);
88+
89+
final downloadTask = islandRef.writeToFile(file);
90+
downloadTask.snapshotEvents.listen((taskSnapshot) {
91+
switch (taskSnapshot.state) {
92+
case TaskState.running:
93+
// TODO: Handle this case.
94+
break;
95+
case TaskState.paused:
96+
// TODO: Handle this case.
97+
break;
98+
case TaskState.success:
99+
// TODO: Handle this case.
100+
break;
101+
case TaskState.canceled:
102+
// TODO: Handle this case.
103+
break;
104+
case TaskState.error:
105+
// TODO: Handle this case.
106+
break;
107+
}
108+
});
109+
```
110+
111+
## Download Data via URL
112+
113+
If you already have download infrastructure based around URLs, or just want
114+
a URL to share, you can get the download URL for a file by calling the
115+
`getDownloadURL()` method on a Cloud Storage reference.
116+
117+
```dart
118+
final imageUrl =
119+
await storageRef.child("users/me/profile.png").getDownloadURL();
120+
```
121+
122+
## Handle Errors
123+
124+
There are a number of reasons why errors may occur on download, including the
125+
file not existing, or the user not having permission to access the desired file.
126+
More information on errors can be found in the [Handle Errors](handle-errors)
127+
section of the docs.
128+
129+
## Full Example
130+
131+
A full example of a download with error handling is shown below:
132+
133+
```dart
134+
final islandRef = storageRef.child("images/island.jpg");
135+
136+
final appDocDir = await getApplicationDocumentsDirectory();
137+
final filePath = "${appDocDir.absolute}/images/island.jpg";
138+
final file = File(filePath);
139+
140+
final downloadTask = islandRef.writeToFile(file);
141+
downloadTask.snapshotEvents.listen((taskSnapshot) {
142+
switch (taskSnapshot.state) {
143+
case TaskState.running:
144+
// TODO: Handle this case.
145+
break;
146+
case TaskState.paused:
147+
// TODO: Handle this case.
148+
break;
149+
case TaskState.success:
150+
// TODO: Handle this case.
151+
break;
152+
case TaskState.canceled:
153+
// TODO: Handle this case.
154+
break;
155+
case TaskState.error:
156+
// TODO: Handle this case.
157+
break;
158+
}
159+
});
160+
```
161+
162+
You can also [get and update metadata](file-metadata) for files that are stored
163+
in Cloud Storage.

0 commit comments

Comments
 (0)