إنشاء مرجع Cloud Storage على الويب

يتم تخزين ملفاتك في حزمة Cloud Storage. ويتم عرض الملفات في هذه الحزمة في بنية هرمية، تمامًا مثل نظام الملفات على القرص الثابت المحلي أو البيانات في "قاعدة بيانات Firebase في الوقت الفعلي". يحصل تطبيقك على إذن الوصول إلى أحد الملفات من خلال إنشاء مرجع إلى أحد الملفات. يمكن بعد ذلك استخدام هذه المراجع لتحميل البيانات أو تنزيلها أو الحصول على البيانات الوصفية أو تحديثها أو حذف الملف. يمكن أن يشير المرجع إلى ملف معين أو إلى عقدة ذات مستوى أعلى في التسلسل الهرمي.

إذا كنت قد استخدمت قاعدة بيانات Firebase في الوقت الفعلي، من المفترض أن تبدو هذه المسارات مألوفة لك تمامًا. ومع ذلك، يتم تخزين بيانات ملفك في Cloud Storage، وليس في قاعدة بيانات الوقت الفعلي.

إنشاء مرجع

لتحميل ملفات أو تنزيلها أو حذفها أو الحصول على البيانات الوصفية أو تحديثها، يجب إنشاء إشارة إلى الملف الذي تريد استخدامه. يمكن اعتبار المرجع على أنه مؤشر إلى ملف في السحابة. المراجع سهلة الفهم، لذا يمكنك إنشاء ما تشاء من المَراجع، كما يمكن إعادة استخدامها في عمليات متعددة.

لإنشاء مرجع، يمكنك الحصول على نسخة افتراضية من خدمة مساحة التخزين باستخدام getStorage()، ثم طلب ref() مع الخدمة كوسيطة. ويشير هذا المرجع إلى جذر حزمتك على Cloud Storage.

واجهة برمجة التطبيقات Web modular API

import { getStorage, ref } from "firebase/storage";

// Get a reference to the storage service, which is used to create references in your storage bucket
const storage = getStorage();

// Create a storage reference from our storage service
const storageRef = ref(storage);

واجهة برمجة التطبيقات لمساحة الاسم على الويب

// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();

// Create a storage reference from our storage service
var storageRef = storage.ref();

يمكنك إنشاء إشارة إلى موقع في أسفل الشجرة، مثل 'images/space.jpg' بالمرور في هذا المسار كوسيطة ثانية عند استدعاء ref().

واجهة برمجة التطبيقات Web modular API

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Create a child reference
const imagesRef = ref(storage, 'images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
const spaceRef = ref(storage, 'images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

واجهة برمجة التطبيقات لمساحة الاسم على الويب

// Create a child reference
var imagesRef = storageRef.child('images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
var spaceRef = storageRef.child('images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

يمكنك أيضًا استخدام السمتَين parent وroot للانتقال إلى أعلى التدرّج الهرمي للملفات. parent للانتقال لمستوى أعلى، بينما ينتقل root إلى الأعلى.

واجهة برمجة التطبيقات Web modular API

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// Parent allows us to move to the parent of a reference
const imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
const rootRef = spaceRef.root;
// rootRef now points to the root

واجهة برمجة التطبيقات لمساحة الاسم على الويب

// Parent allows us to move to the parent of a reference
var imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
var rootRef = spaceRef.root;
// rootRef now points to the root

يمكن ربط child() وparent وroot معًا عدة مرات، لأنّ كل منها يعرض مرجعًا. والاستثناء هو parent من root، وهو null.

واجهة برمجة التطبيقات Web modular API

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// References can be chained together multiple times
const earthRef = ref(spaceRef.parent, 'earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
const nullRef = spaceRef.root.parent;

واجهة برمجة التطبيقات لمساحة الاسم على الويب

// References can be chained together multiple times
var earthRef = spaceRef.parent.child('earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
var nullRef = spaceRef.root.parent;

خصائص المرجع

يمكنك فحص المراجع لفهم الملفات التي تشير إليها بشكل أفضل باستخدام السمات fullPath وname وbucket. تحصل هذه الخصائص على المسار الكامل للملف، واسم الملف، والحزمة التي يتم تخزين الملف فيها.

واجهة برمجة التطبيقات Web modular API

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

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

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;

واجهة برمجة التطبيقات لمساحة الاسم على الويب

// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

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

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;

القيود المفروضة على المراجع

يمكن أن تحتوي المسارات والأسماء المرجعية على أي تسلسل من أحرف يونيكود الصالحة، ولكن يتم فرض قيود معيّنة منها:

  1. يجب أن يتراوح إجمالي طول reference.fullPath بين 1 و1024 بايت عند ترميز UTF-8.
  2. ما مِن أحرف لإرجاع سطر الأوامر أو أحرف لخلاصة السطر.
  3. تجنَّب استخدام # أو [ أو ] أو * أو ?، لأنّ هذه التنسيقات لا تعمل بشكل جيد مع أدوات أخرى مثل قاعدة بيانات Firebase في الوقت الفعلي أو gsutil.

مثال كامل

واجهة برمجة التطبيقات Web modular API

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Points to the root reference
const storageRef = ref(storage);

// Points to 'images'
const imagesRef = ref(storageRef, 'images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
const fileName = 'space.jpg';
const spaceRef = ref(imagesRef, fileName);

// File path is 'images/space.jpg'
const path = spaceRef.fullPath;

// File name is 'space.jpg'
const name = spaceRef.name;

// Points to 'images'
const imagesRefAgain = spaceRef.parent;

واجهة برمجة التطبيقات لمساحة الاسم على الويب

// Points to the root reference
var storageRef = firebase.storage().ref();

// Points to 'images'
var imagesRef = storageRef.child('images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
var fileName = 'space.jpg';
var spaceRef = imagesRef.child(fileName);

// File path is 'images/space.jpg'
var path = spaceRef.fullPath;

// File name is 'space.jpg'
var name = spaceRef.name;

// Points to 'images'
var imagesRef = spaceRef.parent;

بعد ذلك، لنتعرّف على كيفية تحميل الملفات إلى Cloud Storage.