Skip to content

Commit 7af6f58

Browse files
authored
docs(storage): Provide base storage docs (#1395)
* docs(storage): Provide base storage docs * docs(storage): add storage docs * docs(storage): fixes * docs(storage): fixes
1 parent 286f464 commit 7af6f58

File tree

3 files changed

+177
-5
lines changed

3 files changed

+177
-5
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ Firebase offers two cloud-based, client-accessible database solutions that suppo
8686

8787
- [Getting started with Firebase Authentication](docs/auth/getting-started.md)
8888

89+
## Upload files
90+
- [Getting started with Cloud Storage](docs/storage/storage.md)
91+
8992
### Deploy to Firebase Hosting
9093

9194
- [Deploying AngularFire to Firebase Hosting](docs/deploying-angularfire-to-firebase.md)

docs/install-and-setup.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ import { environment } from '../environments/environment';
7676
bootstrap: [ AppComponent ]
7777
})
7878
export class AppModule {}
79-
8079
```
8180

8281
#### Custom FirebaseApp Names
@@ -100,7 +99,7 @@ After adding the AngularFireModule you also need to add modules for the individu
10099
- AngularFirestoreModule
101100
- AngularFireAuthModule
102101
- AngularFireDatabaseModule
103-
- AngularFireStorageModule (Future release)
102+
- AngularFireStorageModule
104103
- AngularFireMessagingModule (Future release)
105104

106105
#### Adding the Firebase Database and Auth Modules
@@ -113,6 +112,7 @@ import { NgModule } from '@angular/core';
113112
import { AppComponent } from './app.component';
114113
import { AngularFireModule } from 'angularfire2';
115114
import { AngularFirestoreModule } from 'angularfire2/firestore';
115+
import { AngularFireStorageModule } from 'angularfire2/storage';
116116
import { AngularFireAuthModule } from 'angularfire2/auth';
117117
import { environment } from '../environments/environment';
118118

@@ -121,13 +121,13 @@ import { environment } from '../environments/environment';
121121
BrowserModule,
122122
AngularFireModule.initializeApp(environment.firebase, 'my-app-name'), // imports firebase/app needed for everything
123123
AngularFirestoreModule, // imports firebase/firestore, only needed for database features
124-
AngularFireAuthModule, // imports firebase/auth, only needed for auth features
124+
AngularFireAuthModule, // imports firebase/auth, only needed for auth features,
125+
AngularFireStorageModule // imports firebase/storage only needed for storage features
125126
],
126127
declarations: [ AppComponent ],
127128
bootstrap: [ AppComponent ]
128129
})
129130
export class AppModule {}
130-
131131
```
132132

133133
### 7. Inject AngularFirestore
@@ -148,7 +148,6 @@ export class AppComponent {
148148

149149
}
150150
}
151-
152151
```
153152

154153
### 8. Bind to a list

docs/storage/storage.md

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# AngularFireStorage
2+
3+
> Cloud Storage is designed to help you quickly and easily store and serve user-generated content, such as photos and videos.
4+
5+
### Import the NgModule
6+
7+
Cloud Storage for AngularFire is contained in the `angularfire2/storage` module namespace. Import the `AngularFireStorageModule` in your `NgModule`. This sets up the `AngularFireStorage` service for dependency injection.
8+
9+
```ts
10+
import { BrowserModule } from '@angular/platform-browser';
11+
import { NgModule } from '@angular/core';
12+
import { AppComponent } from './app.component';
13+
import { AngularFireModule } from 'angularfire2';
14+
import { AngularFireStorageModule } from 'angularfire2/storage';
15+
import { environment } from '../environments/environment';
16+
17+
@NgModule({
18+
imports: [
19+
BrowserModule,
20+
AngularFireModule.initializeApp(environment.firebase),
21+
AngularFireStorageModule
22+
],
23+
declarations: [ AppComponent ],
24+
bootstrap: [ AppComponent ]
25+
})
26+
export class AppModule {}
27+
```
28+
29+
### Injecting the AngularFireStorage service
30+
31+
Once the `AngularFireStorageModule` is registered you can inject the `AngularFireStorage` service.
32+
33+
```ts
34+
import { Component } from '@angular/core';
35+
import { AngularFireStorage } from 'angularfire2/storage';
36+
37+
@Component({
38+
selector: 'app-component',
39+
template: ``
40+
})
41+
export class AppComponent {
42+
constructor(private storage: AngularFireStorage) { }
43+
}
44+
```
45+
46+
### Uploading blobs
47+
48+
There are two options for uploading files.
49+
50+
51+
| method | |
52+
| ---------|--------------------|
53+
| `put(data: Blob, metadata?: storage.UploadMetadata): AngularFireUploadTask` | Starts the upload of the blob to the storage reference's path. Returns an `AngularFireUploadTask` for upload monitoring. |
54+
| `putString(data: string, format?: StringFormat, metadata?: UploadMetadata): AngularFireUploadTask` | Updates an existing item in the array. Accepts a key, database reference, or an unwrapped snapshot. |
55+
56+
```ts
57+
import { Component } from '@angular/core';
58+
import { AngularFireStorage } from 'angularfire2/storage';
59+
60+
@Component({
61+
selector: 'app-root',
62+
template: `
63+
<input type="file" (onchange)="uploadFile($event)">
64+
`
65+
})
66+
export class AppComponent {
67+
constructor(private storage: AngularFireStorage) { }
68+
uploadFile(event) {
69+
const file = event.target.files[0];
70+
const filePath = 'name-your-file-path-here';
71+
const task = this.storage.upload(filePath, file);
72+
}
73+
}
74+
```
75+
76+
### Monitoring upload percentage
77+
78+
An `AngularFireUploadTask` has methods for observing upload percentage as well as the final download URL.
79+
80+
| method | |
81+
| ---------|--------------------|
82+
| `snapshotChanges(): Observable<FirebaseStorage.UploadTaskSnapshot>` | Emits the raw `UploadTaskSnapshot` as the file upload progresses. |
83+
| `percentageChanges(): Observable<number>` | Emits the upload completion percentage. |
84+
| `downloadURL(): Observable<string>` | Emits the download url when available |
85+
86+
#### Example Usage
87+
88+
```ts
89+
@Component({
90+
selector: 'app-root',
91+
template: `
92+
<input type="file" (change)="uploadFile($event)" />
93+
<div>{{ uploadPercent | async }}</div>
94+
<a [href]="downloadURL | async">{{ downloadURL | async }}</a>
95+
`
96+
})
97+
export class AppComponent {
98+
uploadPercent: Observable<number>;
99+
downloadURL: Observable<string>;
100+
constructor(private storage: AngularFireStorage) {}
101+
uploadFile(event) {
102+
const file = event.target.files[0];
103+
const filePath = 'name-your-file-path-here';
104+
const task = this.storage.upload(filePath, file);
105+
106+
// observe percentage changes
107+
this.uploadPercent = task.percentageChanges();
108+
// get notified when the download URL is available
109+
this.downloadURL = task.downloadURL();
110+
}
111+
}
112+
```
113+
114+
### Downloading Files
115+
116+
To download a file you'll need to create a reference and call the `getDownloadURL()` method on an `AngularFireStorageReference`.
117+
118+
```ts
119+
@Component({
120+
selector: 'app-root',
121+
template: `<img [src]="profileUrl | async" />`
122+
})
123+
export class AppComponent {
124+
profileUrl: Observable<string | null>;
125+
constructor(private storage: AngularFireStorage) {
126+
const ref = this.storage.ref('users/davideast.jpg');
127+
this.profileUrl = ref.getDownloadUrl();
128+
}
129+
}
130+
```
131+
132+
### Managing Metadata
133+
134+
Cloud Storage for Firebase allows you to upload and download metadata associated with files. This is useful because you can store important metadata and download it without needing to download the entire file.
135+
136+
#### Downloading metadata
137+
138+
```ts
139+
@Component({
140+
selector: 'app-root',
141+
template: `<pre><code>{{ meta | async }}</code></pre>`
142+
})
143+
export class AppComponent {
144+
meta: Observable<any>;
145+
constructor(private storage: AngularFireStorage) {
146+
const ref = this.storage.ref('users/davideast.jpg');
147+
this.meta = ref.getMetadata();
148+
}
149+
}
150+
```
151+
152+
#### Uploading metadata with files
153+
154+
```ts
155+
@Component({
156+
selector: 'app-root',
157+
template: `
158+
<input type="file" (change)="uploadFile($event)" />
159+
`
160+
})
161+
export class AppComponent {
162+
constructor(private storage: AngularFireStorage) {}
163+
uploadFile(event) {
164+
const file = event.target.files[0];
165+
const filePath = 'name-your-file-path-here';
166+
const ref = this.storage.ref(filePath);
167+
const task = ref.put(file, { customMetadata: { blah: 'blah' } });
168+
}
169+
}
170+
```

0 commit comments

Comments
 (0)