Cloud Storage lets your app save files like photos or documents online. This way, users can access their files from any device anytime.
Cloud Storage in React Native
import storage from '@react-native-firebase/storage'; // Upload a file const reference = storage().ref('path/to/file.jpg'); await reference.putFile(localFilePath); // Download a file URL const url = await reference.getDownloadURL();
ref('path') to point to where the file is stored in the cloud.putFile(localPath) to upload a file from the device.const reference = storage().ref('images/photo1.jpg'); await reference.putFile('/local/path/photo1.jpg');
const reference = storage().ref('docs/report.pdf'); const url = await reference.getDownloadURL();
This app has a button to upload a local image to cloud storage. After uploading, it shows the image from the cloud using the download URL.
import React, {useState} from 'react'; import {View, Button, Image, Text} from 'react-native'; import storage from '@react-native-firebase/storage'; export default function CloudStorageExample() { const [imageUrl, setImageUrl] = useState(null); async function uploadAndGetUrl() { const localPath = '/path/to/local/image.jpg'; const ref = storage().ref('uploads/myImage.jpg'); try { await ref.putFile(localPath); const url = await ref.getDownloadURL(); setImageUrl(url); } catch (e) { setImageUrl(null); } } return ( <View style={{flex:1, justifyContent:'center', alignItems:'center'}}> <Button title="Upload Image" onPress={uploadAndGetUrl} /> {imageUrl ? <Image source={{uri: imageUrl}} style={{width:200, height:200, marginTop:20}} /> : <Text>No image uploaded yet.</Text>} </View> ); }
Make sure to set up Firebase and add the config files to your React Native project before using cloud storage.
Uploading large files may take time; consider showing a loading indicator in real apps.
Always handle errors like permission issues or network problems gracefully.
Cloud Storage lets your app save and get files online easily.
Use ref to point to files and putFile to upload.
Get a file's URL with getDownloadURL to display or download it.
Practice
What is the main purpose of Cloud Storage in a React Native app?
Solution
Step 1: Understand Cloud Storage role
Cloud Storage is used to save and get files online, not for UI or navigation.Step 2: Match purpose with options
Only To save and retrieve files online easily describes saving and retrieving files online.Final Answer:
To save and retrieve files online easily -> Option AQuick Check:
Cloud Storage = Save/Get files online [OK]
- Confusing Cloud Storage with UI components
- Thinking Cloud Storage manages app navigation
- Assuming Cloud Storage handles local state
Which of the following is the correct way to create a reference to a file named image.jpg in Cloud Storage?
const storage = getStorage();
const ref = ?Solution
Step 1: Recall correct syntax for reference
In React Native Firebase, use ref(storage, 'filename') to create a reference.Step 2: Check options for correct function
Only ref(storage, 'image.jpg') uses ref(storage, 'image.jpg') correctly.Final Answer:
ref(storage, 'image.jpg') -> Option CQuick Check:
Reference creation = ref(storage, 'filename') [OK]
- Using storage.ref() which is not valid in React Native
- Calling getRef() which does not exist
- Using storage.getReference() which is incorrect
What will the following code do?
const storage = getStorage();
const fileRef = ref(storage, 'docs/report.pdf');
await uploadBytes(fileRef, fileData);
const url = await getDownloadURL(fileRef);
console.log(url);Solution
Step 1: Analyze uploadBytes usage
The code uploads fileData to 'docs/report.pdf' using uploadBytes with await.Step 2: Analyze getDownloadURL usage
After upload, it gets the file's URL and prints it with console.log.Final Answer:
Upload the file and print its download URL -> Option AQuick Check:
Upload + get URL = print URL [OK]
- Thinking getDownloadURL works before upload
- Missing await causing runtime errors
- Assuming uploadBytes does not upload file
Identify the error in this code snippet for uploading a file:
const storage = getStorage();
const fileRef = ref(storage, 'photos/pic.png');
uploadFile(fileRef, fileData);
const url = await getDownloadURL(fileRef);
console.log(url);Solution
Step 1: Check function names for uploading
The correct function to upload is uploadBytes, not uploadFile.Step 2: Verify other code parts
getDownloadURL can be awaited and ref usage is correct.Final Answer:
uploadFile is not a valid function -> Option DQuick Check:
uploadBytes is correct upload function [OK]
- Using uploadFile instead of uploadBytes
- Forgetting to await uploadBytes
- Misusing getDownloadURL without await
You want to upload a profile picture and then immediately show it in your app. Which sequence of steps is correct?
- Get a reference to the file location
- Upload the file using
uploadBytes - Get the download URL using
getDownloadURL - Display the image using the URL
Solution
Step 1: Create file reference first
You must first get a reference to where the file will be stored.Step 2: Upload file using uploadBytes
Upload the file to that reference location.Step 3: Get download URL after upload
Only after upload can you get the file's URL.Step 4: Display image using URL
Use the URL to show the image in your app.Final Answer:
1 -> 2 -> 3 -> 4 -> Option BQuick Check:
Reference -> Upload -> URL -> Display [OK]
- Trying to get URL before uploading
- Uploading before creating reference
- Displaying image without URL
Ready to prove it?
Pass the test and this pattern moves unknown
