Cloud Storage และฟังก์ชัน
ผสานรวม Firebase Cloud Storage สำหรับอัปโหลดและดาวน์โหลดไฟล์ พร้อมสำรวจ Cloud Functions สำหรับตรรกะส่วนหลังแบบไร้เซิร์ฟเวอร์
Cloud Storage และฟังก์ชัน เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flutter Mobile Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Cloud Storage: Your App's Files
Welcome to Firebase Cloud Storage! This service allows your app to store and serve user-generated content, such as photos, videos, and documents.
Think of it as a powerful, scalable cloud-based file system for your mobile application. It's built for scale and integrates seamlessly with other Firebase services.
Storage Buckets & Rules
Files in Cloud Storage are organized into buckets. Each Firebase project has a default bucket, but you can create more.
Crucially, you define security rules for your storage bucket. These rules determine who can read, write, or delete files, ensuring your data is protected.
allow read, write: if request.auth != null;(authenticated users)allow read;(public read)
Preparing for File Uploads
To upload files from your Flutter app, you'll use the firebase_storage package. First, you need to get a reference to where you want to store the file in your bucket.
The actual process of picking a file from the device (e.g., gallery or camera) typically involves another package like image_picker or file_picker, which we'll assume for our example.
Code: Uploading Data
Here's how to upload a simple string of data to Firebase Cloud Storage. In a real app, you'd replace the string with bytes from a file picked by the user.
(Firebase initialization is assumed for this runnable snippet)
import 'package:firebase_storage/firebase_storage.dart';
import 'dart:typed_data';
void main() async {
// In a real Flutter app, Firebase must be initialized first:
// await Firebase.initializeApp();
final storageRef = FirebaseStorage.instance.ref();
final fileRef = storageRef.child("coddykit_uploads/greeting.txt");
try {
final String content = "Hello from CoddyKit Storage!";
final Uint8List data = Uint8List.fromList(content.codeUnits);
await fileRef.putData(data);
print("File 'greeting.txt' uploaded!");
} catch (e) {
print("Upload error: $e");
}
}Getting Download URLs
After uploading a file, you often need a public URL to access it, for example, to display an image in your app or share a document.
Firebase Storage provides a method to get this download URL. You can then store this URL in a database like Cloud Firestore for easy retrieval and display.
Code: Retrieve Download URL
Once a file is uploaded, you can easily retrieve its public download URL. This URL can be used directly in widgets like Image.network().
(Firebase initialization is assumed for this runnable snippet)
import 'package:firebase_storage/firebase_storage.dart';
void main() async {
// In a real Flutter app, Firebase must be initialized first.
final storageRef = FirebaseStorage.instance.ref();
// Reference the same file we uploaded previously
final fileRef = storageRef.child("coddykit_uploads/greeting.txt");
try {
final String downloadURL = await fileRef.getDownloadURL();
print("Download URL for 'greeting.txt':");
print(downloadURL);
} catch (e) {
print("Error getting URL: $e");
print("Ensure 'greeting.txt' exists in storage.");
}
}Cloud Functions: Serverless Backend
Firebase Cloud Functions let you run backend code in response to events triggered by Firebase features and HTTPS requests, all without managing servers.
This means you can extend your app's functionality with custom logic that runs in a secure, scalable cloud environment.
Function Types & Triggers
Cloud Functions can be triggered by various events:
- HTTP Triggers: Respond to standard HTTP requests (like a REST API endpoint).
- Database Triggers: Run code when data is created, updated, or deleted in Cloud Firestore or Realtime Database.
- Storage Triggers: Respond to file uploads, deletions, or metadata changes in Cloud Storage.
- Auth Triggers: Execute code when users are created or deleted.
Calling Functions from Flutter
To interact with Cloud Functions from your Flutter app, you'll use the cloud_functions package. You can call an HTTP-triggered function by its name and pass data to it.
The function executes on the server, and your app receives the result.
(Firebase initialization is assumed, and a function named 'sayHello' is expected on the backend.)
import 'package:cloud_functions/cloud_functions.dart';
void main() async {
// In a real Flutter app, Firebase must be initialized first.
try {
// Call a Cloud Function named 'sayHello'
final HttpsCallable callable =
FirebaseFunctions.instance.httpsCallable('sayHello');
final result = await callable.call(<String, dynamic>{
'name': 'CoddyKit User',
});
print("Function response: ${result.data}");
} on FirebaseFunctionsException catch (e) {
print("Cloud Function error: ${e.code} - ${e.message}");
} catch (e) {
print("General error calling function: $e");
}
}Quick Check: Firebase Services
Which of the following statements about Firebase Cloud Storage and Cloud Functions are TRUE?
Recap: Storage & Functions
Great job! In this lesson, you learned about:
- Firebase Cloud Storage: A scalable service for storing user-generated content like files.
- Storage Security Rules: How to secure your files by defining access permissions.
- Uploading & Downloading: Using
firebase_storageto manage files and retrieve download URLs. - Cloud Functions: A serverless platform for running backend code in response to events or HTTP requests.
- Integration: How to call Cloud Functions from your Flutter app using
cloud_functions.
These powerful services allow you to add robust file management and custom backend logic to your Flutter applications!
คำถามที่พบบ่อย
บทเรียน “Cloud Storage และฟังก์ชัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “Cloud Storage และฟังก์ชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “Cloud Storage และฟังก์ชัน”
ผสานรวม Firebase Cloud Storage สำหรับอัปโหลดและดาวน์โหลดไฟล์ พร้อมสำรวจ Cloud Functions สำหรับตรรกะส่วนหลังแบบไร้เซิร์ฟเวอร์ คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “Cloud Storage และฟังก์ชัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม
ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่า Firebase และการยืนยันตัวตน
- ฐานข้อมูล Cloud Firestore
- Cloud Storage และฟังก์ชัน
- การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน