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

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

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

یک مرجع ایجاد کنید

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

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

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

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

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;

محدودیت در مراجع

مسیرهای مرجع و نام‌ها می‌توانند حاوی هر دنباله‌ای از کاراکترهای معتبر یونیکد باشند، اما محدودیت‌های خاصی اعمال می‌شود از جمله:

  1. طول کل reference.fullPath باید بین 1 تا 1024 بایت باشد که UTF-8 کدگذاری شود.
  2. بدون کاراکترهای Carriage Return یا Line Feed.
  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;

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;

در مرحله بعد، بیایید نحوه آپلود فایل ها در فضای ذخیره سازی ابری را بیاموزیم.