یک مرجع Cloud Storage در وب ایجاد کنید

فایل‌های شما در یک مخزن Cloud Storage Bucket) ذخیره می‌شوند. فایل‌های موجود در این مخزن، درست مانند سیستم فایل روی هارد دیسک محلی شما یا داده‌های موجود در Firebase Realtime Database ، در یک ساختار سلسله مراتبی ارائه می‌شوند. با ایجاد یک ارجاع به یک فایل، برنامه شما به آن دسترسی پیدا می‌کند. سپس می‌توان از این ارجاع‌ها برای آپلود یا دانلود داده‌ها، دریافت یا به‌روزرسانی فراداده یا حذف فایل استفاده کرد. یک ارجاع می‌تواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.

اگر از Firebase Realtime Database استفاده کرده باشید، این مسیرها باید برای شما بسیار آشنا به نظر برسند. با این حال، داده‌های فایل شما در Cloud Storage ذخیره می‌شوند، نه در Realtime Database .

ایجاد یک مرجع

برای آپلود یا دانلود فایل‌ها، حذف فایل‌ها، یا دریافت یا به‌روزرسانی فراداده‌ها، باید یک ارجاع به فایلی که می‌خواهید روی آن عملیات انجام دهید، ایجاد کنید. یک ارجاع را می‌توان به عنوان یک اشاره‌گر به یک فایل در فضای ابری در نظر گرفت. ارجاع‌ها سبک هستند، بنابراین می‌توانید هر تعداد که نیاز دارید ایجاد کنید و همچنین برای چندین عملیات قابل استفاده مجدد هستند.

برای ایجاد یک مرجع، با استفاده از 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();

شما می‌توانید با ارسال این مسیر به عنوان آرگومان دوم هنگام فراخوانی ref() ‎، به مکانی در پایین درخت، مثلاً 'images/space.jpg' ارجاعی ایجاد کنید.

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 بهتر درک کنید. این ویژگی‌ها مسیر کامل فایل، نام فایل و 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 هنگام کدگذاری UTF-8 باید بین ۱ تا ۱۰۲۴ بایت باشد.
  2. بدون کاراکترهای بازگشت به خط یا تغذیه خط.
  3. از استفاده از # ، [ ، ] ، * یا ? خودداری کنید، زیرا این علائم با ابزارهای دیگری مانند Firebase Realtime Database یا 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;

در ادامه، نحوه آپلود فایل‌ها در Cloud Storage را بررسی خواهیم کرد.