צור הפניה ל-Cloud Storage עם Cloud Storage עבור C++

הקבצים שלך מאוחסנים בדלי של Cloud Storage . הקבצים בדלי זה מוצגים במבנה היררכי, בדיוק כמו מערכת הקבצים בדיסק הקשיח המקומי שלך, או הנתונים במסד הנתונים של Firebase Realtime. על ידי יצירת הפניה לקובץ, האפליקציה שלך מקבלת גישה אליו. לאחר מכן ניתן להשתמש בהפניות אלה כדי להעלות או להוריד נתונים, לקבל או לעדכן מטא נתונים או למחוק את הקובץ. הפניה יכולה להצביע על קובץ ספציפי או על צומת ברמה גבוהה יותר בהיררכיה.

אם השתמשת במסד הנתונים בזמן אמת של Firebase , הנתיבים האלה אמורים להיראות לך מוכרים מאוד. עם זאת, נתוני הקבצים שלך מאוחסנים ב-Cloud Storage, לא במסד הנתונים בזמן אמת.

צור הפניה

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

הפניות נוצרות משירות storage באפליקציית Firebase שלך ​​על ידי קריאה לשיטת GetReferenceFromUrl() והעברת כתובת URL בצורת gs://<your-cloud-storage-bucket> . אתה יכול למצוא כתובת אתר זו בקטע אחסון של מסוף Firebase .

// Get a reference to the storage service, using the default Firebase App
Storage* storage = Storage::GetInstance(app);

// Create a Cloud Storage reference from our storage service
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

אתה יכול ליצור הפניה למיקום נמוך יותר בעץ, אמור 'images/space.jpg' , על ידי שימוש בשיטת child על הפניה קיימת.

// Create a child reference
// images_ref now points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Child references can also take paths delimited by '/'
// space_ref now points to "images/space.jpg"
// images_ref still points to "images"
StorageReference space_ref = storage_ref.Child("images/space.jpg");

// This is equivalent to creating the full reference
StorageReference space_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");

אתה יכול גם להשתמש בשיטות Parent Root כדי לנווט למעלה בהיררכיית הקבצים שלנו. Parent מנווט למעלה רמה אחת, בעוד Root כל הדרך למעלה.

// Parent allows us to move to the parent of a reference
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root allows us to move all the way back to the top of our bucket
// root_ref now points to the root
StorageReference root_ref = space_ref.Root();

ניתן לשרשר Child , Parent Root יחד מספר פעמים, כאשר כל אחת מהן מחזירה הפניה. היוצא מן הכלל הוא Parent של Root , שהוא StorageReference לא חוקי.

// References can be chained together multiple times
// earth_ref points to "images/earth.jpg"
StorageReference earth_ref = space_ref.Parent().Child("earth.jpg");

// null_ref is null, since the parent of root is an invalid StorageReference
StorageReference null_ref = space_ref.Root().Parent();

שיטות התייחסות

אתה יכול לבדוק הפניות כדי להבין טוב יותר את הקבצים שהם מצביעים עליהם באמצעות שיטות full_path , name ו- bucket . שיטות אלה מקבלים את הנתיב המלא של הקובץ, השם והדלי.

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
space_ref.full_path();

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
space_ref.name();

// Reference's bucket is the name of the Cloud Storage bucket where files are stored
space_ref.bucket();

מגבלות על הפניות

נתיבים ושמות הפניה יכולים להכיל כל רצף של תווי Unicode חוקי, אך מוטלות הגבלות מסוימות כולל:

  1. האורך הכולל של reference.fullPath חייב להיות בין 1 ל-1024 בתים בעת קידוד UTF-8.
  2. אין תווים להחזרת כרכרה או הזנת שורה.
  3. הימנע משימוש ב- # , [ , ] , * או ? , שכן אלה אינם פועלים היטב עם כלים אחרים כגון Firebase Realtime Database או gsutil .

דוגמה מלאה

Storage* storage = Storage::GetInstance(app);

// Points to the root reference
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
std::string filename = "space.jpg";
StorageReference space_ref = images_ref.Child(filename);

// File path is "images/space.jpg"
std::string path = space_ref.full_path()

// File name is "space.jpg"
std::string name = space_ref.name()

// Points to "images"
StorageReference images_ref = space_ref.Parent();

הצעדים הבאים

לאחר מכן, נלמד כיצד להעלות קבצים ל-Cloud Storage.