إنشاء مرجع للتخزين السحابي على الويب

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

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

إنشاء مرجع

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

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

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);

Web namespaced API

// 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"

Web namespaced API

// 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

Web namespaced API

// 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;

Web namespaced API

// 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;

Web namespaced API

// 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;

القيود على المراجع

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

  1. يجب أن يتراوح إجمالي طول reference.fullPath بين 1 و1024 بايت عند ترميز UTF-8.
  2. لا توجد أحرف إرجاع أو تغذية الأسطر.
  3. تجنب استخدام # أو [ أو ] أو * أو ? ، حيث إنها لا تعمل بشكل جيد مع الأدوات الأخرى مثل Firebase Realtime Database أو 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;

Web namespaced API

// 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.