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

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

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

إنشاء مرجع

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

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

Web

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

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

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

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

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

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

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

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

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

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

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

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

بعد ذلك، دعنا نتعلم كيفية تحميل الملفات إلى التخزين السحابي: