Cloud Storage for Firebase מאפשר לך להעלות במהירות ובקלות קבצים ל- Cloud Storage שסופק ומנוהל על ידי Firebase.
צור הפניה
כדי להעלות קובץ, תחילה צור הפניה ל-Cloud Storage למיקום ב-Cloud Storage שאליו ברצונך להעלות את הקובץ.
אתה יכול ליצור הפניה על ידי הוספת נתיבים צאצאים לשורש של דלי ה-Cloud Storage שלך:
// Create a root reference StorageReference storage_ref = storage->GetReference(); // Create a reference to "mountains.jpg" StorageReference mountains_ref = storage_ref.Child("mountains.jpg"); // Create a reference to 'images/mountains.jpg' StorageReference mountain_images_ref = storage_ref.Child("images/mountains.jpg"); // While the file names are the same, the references point to different files mountains_ref.name() == mountain_images_ref.name(); // true mountains_ref.full_path() == mountain_images_ref.full_path(); // false
אינך יכול להעלות נתונים עם הפניה לשורש של דלי ה-Cloud Storage שלך. ההפניה שלך חייבת להפנות לכתובת אתר בת.
העלה קבצים
ברגע שיש לך הפניה, תוכל להעלות קבצים ל-Cloud Storage בשתי דרכים:
- העלה ממאגר בתים בזיכרון
- העלה מנתיב קובץ המייצג קובץ במכשיר
העלה מנתונים בזיכרון
שיטת PutData()
היא הדרך הפשוטה ביותר להעלות קובץ ל-Cloud Storage. PutData()
לוקח מאגר בתים ומחזיר Future<Metadata>
שיכיל מידע על הקובץ כשה-Future יסתיים. אתה יכול להשתמש Controller
כדי לנהל את ההעלאה שלך ולנטר את הסטטוס שלה.
// Data in memory const size_t kByteBufferSize = ... uint8_t byte_buffer[kByteBufferSize] = { ... }; // Create a reference to the file you want to upload StorageReference rivers_ref = storage_ref.Child("images/rivers.jpg"); // Upload the file to the path "images/rivers.jpg" Futurefuture = rivers_ref.PutBytes(byte_buffer, kByteBufferSize);
בשלב שהבקשה הוגשה אך עלינו לחכות להשלמה של העתיד לפני העלאת הקובץ. מכיוון שמשחקים בדרך כלל פועלים בלולאה, והם פחות מונעים מהתקשרות חוזרת מיישומים אחרים, בדרך כלל תבצע סקר לצורך השלמתו.
if (future.status() != firebase::kFutureStatusPending) { if (future.status() != firebase::kFutureStatusComplete) { LogMessage("ERROR: GetData() returned an invalid future."); // Handle the error... } else if (future.Error() != firebase::storage::kErrorNone) { LogMessage("ERROR: GetData() returned error %d: %s", future.Error(), future.error_message()); // Handle the error... } } else { // Metadata contains file metadata such as size, content-type, and download URL. Metadata* metadata = future.Result(); std::string download_url = metadata->download_url(); } }
העלה מקובץ מקומי
ניתן להעלות קבצים מקומיים במכשירים, כגון תמונות וסרטונים מהמצלמה, בשיטת PutFile()
. PutFile()
לוקח std::string
המייצג את הנתיב לקובץ ומחזיר Future<Metadata>
שיכיל מידע על הקובץ כשה-Future יסתיים. אתה יכול להשתמש Controller
כדי לנהל את ההעלאה שלך ולנטר את הסטטוס שלה.
// File located on disk std::string local_file = ... // Create a reference to the file you want to upload StorageReference rivers_ref = storage_ref.Child("images/rivers.jpg"); // Upload the file to the path "images/rivers.jpg" Futurefuture = rivers_ref.PutFile(localFile); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // Metadata contains file metadata such as size, content-type, and download URL. Metadata* metadata = future.Result(); std::string download_url = metadata->download_url(); }
אם אתה רוצה לנהל באופן פעיל את ההעלאה שלך, אתה יכול לספק Controller
לשיטות PutFile()
או PutBytes()
. זה מאפשר לך להשתמש בבקר כדי לצפות בפעולת ההעלאה המתמשכת. ראה ניהול העלאות למידע נוסף.
הוסף מטא נתונים של קובץ
אתה יכול גם לכלול מטא נתונים בעת העלאת קבצים. מטא נתונים זה מכיל מאפייני מטא נתונים טיפוסיים של קבצים כגון name
, size
ו- content_type
(המכונה בדרך כלל סוג MIME). שיטת PutFile()
מסיקה אוטומטית את סוג התוכן מסיומת שם הקובץ, אך ניתן לעקוף את הסוג שזוהה אוטומטית על ידי ציון content_type
במטא-נתונים. אם אינך מספק content_type
ו-Cloud Storage לא יכול להסיק ברירת מחדל מסיומת הקובץ, Cloud Storage משתמש application/octet-stream
. עיין בסעיף השתמש במטא נתונים של קבצים למידע נוסף על מטא נתונים של קבצים.
// Create storage reference StorageReference mountains_ref = storage_ref.Child("images/mountains.jpg"); // Create file metadata including the content type StorageMetadata metadata; metadata.set_content_type("image/jpeg"); // Upload data and metadata mountains_ref.PutBytes(data, metadata); // Upload file and metadata mountains_ref.PutFile(local_file, metadata);
נהל העלאות
בנוסף להתחלת העלאות, אתה יכול להשהות, להמשיך ולבטל העלאות באמצעות השיטות Pause()
, Resume()
ו- Cancel()
ב- Controller
, שאותן תוכל להעביר לשיטות PutBytes()
או PutFile()
באופן אופציונלי.
// Start uploading a file firebase::storage::Controller controller; storage_ref.Child("images/mountains.jpg").PutFile(local_file, nullptr, &controller); // Pause the upload controller.Pause(); // Resume the upload controller.Resume(); // Cancel the upload controller.Cancel();
עקוב אחר התקדמות ההעלאה
ניתן לצרף מאזינים להעלאות על מנת לעקוב אחר התקדמות ההעלאה.
class MyListener : public firebase::storage::Listener { public: virtual void OnProgress(firebase::storage::Controller* controller) { // A progress event occurred } }; { // Start uploading a file MyEventListener my_listener; storage_ref.Child("images/mountains.jpg").PutFile(local_file, my_listener); }
טיפול בשגיאות
ישנן מספר סיבות לכך שגיאות עלולות להתרחש בהעלאה, כולל הקובץ המקומי אינו קיים, או שלמשתמש אין הרשאה להעלות את הקובץ הרצוי. תוכל למצוא מידע נוסף על שגיאות בקטע טיפול בשגיאות במסמכים.
הצעדים הבאים
כעת, לאחר שהעלית קבצים, בואו ללמוד כיצד להוריד אותם מ-Cloud Storage.