העלאת קבצים באמצעות Cloud Storage ל-C++

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). ההפניה חייבת להפנות לכתובת URL של צאצא.

העלאת קבצים

כשיהיה לך קובץ עזר, תהיה לך אפשרות להעלות קבצים אל Cloud Storage בשתי דרכים:

  1. העלאה ממאגר נתונים זמני של בייטים בזיכרון
  2. העלאה מנתיב קובץ שמייצג קובץ במכשיר

העלאה מנתונים שבזיכרון

השיטה 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"
Future future = rivers_ref.PutBytes(byte_buffer, kByteBufferSize);

בשלב זה הבקשה הוגשה, אבל עלינו להמתין ל יושלם לפני שהקובץ יועלה. מכיוון שמשחקים בדרך כלל פועלים לולאה, מעודדים פחות קריאה חוזרת (callback) מאשר אפליקציות אחרות, ובדרך כלל תשאלו לסיום.

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> שיכיל מידע על הקובץ כשהעתיד יסתיים. אפשר להשתמש 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"
Future future = 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.