إنشاء مرجع Cloud Storage باستخدام Cloud Storage لـ C++
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يتم تخزين ملفاتك في حزمة Cloud Storage. يتم عرض الملفات في هذا الحزمة في بنية هرمية، تمامًا مثل نظام الملفات على القرص الصلب المحلي أو البيانات في Firebase Realtime Database.
من خلال إنشاء مرجع إلى ملف، يمكن لتطبيقك الوصول إليه. ويمكن استخدام هذه المراجع لتحميل البيانات أو تنزيلها أو الحصول على البيانات الوصفية أو تعديلها أو حذف الملف. يمكن أن يشير المرجع إلى ملف معيّن أو إلى عقدة ذات مستوى أعلى في التدرّج الهرمي.
إذا كنت قد استخدمت Firebase Realtime Database، من المفترض أن تكون هذه المسارات مألوفة جدًا بالنسبة إليك. ومع ذلك، يتم تخزين بيانات ملفاتك في Cloud Storage، وليس في Realtime Database.
إنشاء مرجع
إنشاء مرجع لتحميل ملف أو تنزيله أو حذفه،
أو للحصول على البيانات الوصفية الخاصة به أو تعديلها يمكن اعتبار المرجع
مؤشرًا إلى ملف في السحابة الإلكترونية. المراجع بسيطة، لذا يمكنك إنشاء العدد الذي تحتاجه منها. ويمكن أيضًا إعادة استخدامها في عمليات متعددة.
يتم إنشاء المراجع من خدمة storage في تطبيقك على Firebase من خلال استدعاء طريقة GetReferenceFromUrl() وتمرير عنوان URL بالتنسيق gs://<your-cloud-storage-bucket>. يمكنك العثور على عنوان URL هذا في قسم مساحة التخزين ضمن وحدة تحكّم Firebase.
// Get a reference to the storage service, using the default Firebase AppStorage*storage=Storage::GetInstance(app);// Create a Cloud Storage reference from our storage serviceStorageReferencestorage_ref=storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
يمكنك إنشاء مرجع إلى موقع أدنى في الشجرة،
على سبيل المثال 'images/space.jpg'، باستخدام طريقة child على مرجع حالي.
// Create a child reference// images_ref now points to "images"StorageReferenceimages_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"StorageReferencespace_ref=storage_ref.Child("images/space.jpg");// This is equivalent to creating the full referenceStorageReferencespace_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'StorageReferenceimages_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 rootStorageReferenceroot_ref=space_ref.Root();
يمكن ربط Child وParent وRoot معًا عدة مرات، لأنّ كلّ منها يعرض مرجعًا. الاستثناء هو Parent من Root، وهو StorageReference غير صالح.
// References can be chained together multiple times// earth_ref points to "images/earth.jpg"StorageReferenceearth_ref=space_ref.Parent().Child("earth.jpg");// null_ref is null, since the parent of root is an invalid StorageReferenceStorageReferencenull_ref=space_ref.Root().Parent();
طُرق الإشارة إلى المراجع
يمكنك فحص المراجع لفهم الملفات التي تشير إليها بشكل أفضل باستخدام الطرق full_path وname وbucket. تستردّ هذه الطرق المسار الكامل للملف واسمه وحزمة التخزين.
// Reference's path is: "images/space.jpg"// This is analogous to a file path on diskspace_ref.full_path();// Reference's name is the last segment of the full path: "space.jpg"// This is analogous to the file namespace_ref.name();// Reference's bucket is the name of the Cloud Storage bucket where files are storedspace_ref.bucket();
القيود المفروضة على المراجع
يمكن أن تحتوي مسارات المراجع وأسماؤها على أي تسلسل من أحرف Unicode الصالحة،
ولكن يتم فرض قيود معيّنة، بما في ذلك:
يجب أن يتراوح الطول الإجمالي لـ reference.fullPath بين بايت واحد و1024 بايت عند استخدام ترميز UTF-8.
يجب عدم استخدام أحرف الرجوع إلى أول السطر أو نهاية السطر.
Storage*storage=Storage::GetInstance(app);// Points to the root referenceStorageReferencestorage_ref=storage->GetReferenceFromUrl("gs://<your-bucket-name>");// Points to "images"StorageReferenceimages_ref=storage_ref.Child("images");// Points to "images/space.jpg"// Note that you can use variables to create child valuesstd::stringfilename="space.jpg";StorageReferencespace_ref=images_ref.Child(filename);// File path is "images/space.jpg"std::stringpath=space_ref.full_path()// File name is "space.jpg"std::stringname=space_ref.name()// Points to "images"StorageReferenceimages_ref=space_ref.Parent();
الخطوات التالية
بعد ذلك، سنتعرّف على كيفية
تحميل الملفات إلى
Cloud Storage.
تاريخ التعديل الأخير: 2025-09-06 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-09-06 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["\u003cbr /\u003e\n\nYour files are stored in a\n[Cloud Storage](//cloud.google.com/storage) bucket. The\nfiles in this bucket are presented in a hierarchical structure, just like the\nfile system on your local hard disk, or the data in the Firebase Realtime Database.\nBy creating a reference to a file, your app gains access to it. These references\ncan then be used to upload or download data, get or update metadata or delete\nthe file. A reference can either point to a specific file or to a higher level\nnode in the hierarchy.\n\nIf you've used the [Firebase Realtime Database](/docs/database), these paths should\nseem very familiar to you. However, your file data is stored in\nCloud Storage, **not** in the Realtime Database.\n\nCreate a Reference\n\nCreate a reference to upload, download, or delete a file,\nor to get or update its metadata. A reference\ncan be thought of as a pointer to a file in the cloud. References are\nlightweight, so you can create as many as you need. They are also reusable for\nmultiple operations.\n\nReferences are created from the `storage` service on your Firebase app by\ncalling the `GetReferenceFromUrl()` method and passing in a URL of the form\n`gs://\u003cyour-cloud-storage-bucket\u003e`. You can find this URL in the\n*Storage* section of the [Firebase console](//console.firebase.google.com/). \n\n```c++\n// Get a reference to the storage service, using the default Firebase App\nStorage* storage = Storage::GetInstance(app);\n\n// Create a Cloud Storage reference from our storage service\nStorageReference storage_ref = storage-\u003eGetReferenceFromUrl(\"gs://\u003cyour-cloud-storage-bucket\u003e\");\n```\n\nYou can create a reference to a location lower in the tree,\nsay `'images/space.jpg'`, by using the `child` method on an existing reference. \n\n```c++\n// Create a child reference\n// images_ref now points to \"images\"\nStorageReference images_ref = storage_ref.Child(\"images\");\n\n// Child references can also take paths delimited by '/'\n// space_ref now points to \"images/space.jpg\"\n// images_ref still points to \"images\"\nStorageReference space_ref = storage_ref.Child(\"images/space.jpg\");\n\n// This is equivalent to creating the full reference\nStorageReference space_ref = storage.GetReferenceFromUrl(\"gs://\u003cyour-cloud-storage-bucket\u003e/images/space.jpg\");\n```\n\nNavigate with References\n\nYou can also use the `Parent` and `Root` methods to navigate up in our file\nhierarchy. `Parent` navigates up one level, while `Root` navigates all the way\nto the top. \n\n```c++\n// Parent allows us to move to the parent of a reference\n// images_ref now points to 'images'\nStorageReference images_ref = space_ref.Parent();\n\n// Root allows us to move all the way back to the top of our bucket\n// root_ref now points to the root\nStorageReference root_ref = space_ref.Root();\n```\n\n`Child`, `Parent`, and `Root` can be chained together multiple times, as\neach returns a reference. The exception is the `Parent` of `Root`, which\nis an invalid `StorageReference`. \n\n```c++\n// References can be chained together multiple times\n// earth_ref points to \"images/earth.jpg\"\nStorageReference earth_ref = space_ref.Parent().Child(\"earth.jpg\");\n\n// null_ref is null, since the parent of root is an invalid StorageReference\nStorageReference null_ref = space_ref.Root().Parent();\n```\n\nReference Methods\n\nYou can inspect references to better understand the files they point to using\nthe `full_path`, `name`, and `bucket` methods. These methods get the file's full\npath, name, and bucket. \n\n```c++\n// Reference's path is: \"images/space.jpg\"\n// This is analogous to a file path on disk\nspace_ref.full_path();\n\n// Reference's name is the last segment of the full path: \"space.jpg\"\n// This is analogous to the file name\nspace_ref.name();\n\n// Reference's bucket is the name of the Cloud Storage bucket where files are stored\nspace_ref.bucket();\n```\n\nLimitations on References\n\nReference paths and names can contain any sequence of valid Unicode characters,\nbut certain restrictions are imposed including:\n\n1. Total length of reference.fullPath must be between 1 and 1024 bytes when UTF-8 encoded.\n2. No Carriage Return or Line Feed characters.\n3. Avoid using `#`, `[`, `]`, `*`, or `?`, as these do not work well with other tools such as the [Firebase Realtime Database](/docs/database) or [gsutil](https://cloud.google.com/storage/docs/gsutil).\n\nFull Example \n\n```c++\nStorage* storage = Storage::GetInstance(app);\n\n// Points to the root reference\nStorageReference storage_ref = storage-\u003eGetReferenceFromUrl(\"gs://\u003cyour-bucket-name\u003e\");\n\n// Points to \"images\"\nStorageReference images_ref = storage_ref.Child(\"images\");\n\n// Points to \"images/space.jpg\"\n// Note that you can use variables to create child values\nstd::string filename = \"space.jpg\";\nStorageReference space_ref = images_ref.Child(filename);\n\n// File path is \"images/space.jpg\"\nstd::string path = space_ref.full_path()\n\n// File name is \"space.jpg\"\nstd::string name = space_ref.name()\n\n// Points to \"images\"\nStorageReference images_ref = space_ref.Parent();\n```\n\nNext Steps\n\nNext, let's learn how to\n[upload files](/docs/storage/cpp/upload-files) to\nCloud Storage."]]