Cloud Storage for Firebase به شما این امکان را می دهد که به سرعت و به راحتی فایل ها را از یک سطل Cloud Storage که توسط Firebase تهیه و مدیریت می شود دانلود کنید.
یک مرجع ایجاد کنید
برای دانلود یک فایل، ابتدا یک مرجع Cloud Storage برای فایلی که می خواهید دانلود کنید ایجاد کنید .
میتوانید با اضافه کردن مسیرهای فرزند به ریشه سطل فضای ذخیرهسازی ابری خود، یک مرجع ایجاد کنید، یا میتوانید از یک نشانی اینترنتی gs://
یا https://
موجود که به یک شی در فضای ذخیرهسازی ابری ارجاع میدهد، یک مرجع ایجاد کنید.
Web modular API
import { getStorage, ref } from "firebase/storage"; // Create a reference with an initial file path and name const storage = getStorage(); const pathReference = ref(storage, 'images/stars.jpg'); // Create a reference from a Google Cloud Storage URI const gsReference = ref(storage, 'gs://bucket/images/stars.jpg'); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! const httpsReference = ref(storage, 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
Web namespaced API
// Create a reference with an initial file path and name var storage = firebase.storage(); var pathReference = storage.ref('images/stars.jpg'); // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg'); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
دانلود داده ها از طریق URL
میتوانید URL دانلود یک فایل را با فراخوانی متد getDownloadURL()
در مرجع Cloud Storage دریافت کنید.
Web modular API
import { getStorage, ref, getDownloadURL } from "firebase/storage"; const storage = getStorage(); getDownloadURL(ref(storage, 'images/stars.jpg')) .then((url) => { // `url` is the download URL for 'images/stars.jpg' // This can be downloaded directly: const xhr = new XMLHttpRequest(); xhr.responseType = 'blob'; xhr.onload = (event) => { const blob = xhr.response; }; xhr.open('GET', url); xhr.send(); // Or inserted into an <img> element const img = document.getElementById('myimg'); img.setAttribute('src', url); }) .catch((error) => { // Handle any errors });
Web namespaced API
storageRef.child('images/stars.jpg').getDownloadURL() .then((url) => { // `url` is the download URL for 'images/stars.jpg' // This can be downloaded directly: var xhr = new XMLHttpRequest(); xhr.responseType = 'blob'; xhr.onload = (event) => { var blob = xhr.response; }; xhr.open('GET', url); xhr.send(); // Or inserted into an <img> element var img = document.getElementById('myimg'); img.setAttribute('src', url); }) .catch((error) => { // Handle any errors });
داده ها را مستقیماً از SDK دانلود کنید
از نسخه 9.5 و بالاتر، SDK این توابع را برای دانلود مستقیم فراهم می کند:
با استفاده از این توابع، می توانید بارگیری از یک URL را دور بزنید و در عوض داده های کد خود را برگردانید. این امکان کنترل دسترسی دقیق تر از طریق قوانین امنیتی Firebase را فراهم می کند.
پیکربندی CORS
برای دانلود مستقیم داده ها در مرورگر، باید سطل فضای ذخیره سازی ابری خود را برای دسترسی متقاطع (CORS) پیکربندی کنید. این را می توان با ابزار خط فرمان gsutil
انجام داد که می توانید آن را از اینجا نصب کنید .
اگر هیچ محدودیتی بر اساس دامنه نمیخواهید (متداولترین سناریو)، این JSON را در فایلی با نام cors.json
کپی کنید:
[ { "origin": ["*"], "method": ["GET"], "maxAgeSeconds": 3600 } ]
gsutil cors set cors.json gs://<your-cloud-storage-bucket>
را برای اعمال این محدودیت ها اجرا کنید.
برای اطلاعات بیشتر، به مستندات Google Cloud Storage مراجعه کنید.
رسیدگی به خطاها
دلایل متعددی وجود دارد که ممکن است هنگام دانلود خطا رخ دهد، از جمله اینکه فایل موجود نیست یا کاربر اجازه دسترسی به فایل مورد نظر را ندارد. اطلاعات بیشتر در مورد خطاها را می توانید در بخش Handle Errors در اسناد پیدا کنید.
مثال کامل
یک مثال کامل از دانلود با مدیریت خطا در زیر نشان داده شده است:
Web modular API
import { getStorage, ref, getDownloadURL } from "firebase/storage"; // Create a reference to the file we want to download const storage = getStorage(); const starsRef = ref(storage, 'images/stars.jpg'); // Get the download URL getDownloadURL(starsRef) .then((url) => { // Insert url into an <img> tag to "download" }) .catch((error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { case 'storage/object-not-found': // File doesn't exist break; case 'storage/unauthorized': // User doesn't have permission to access the object break; case 'storage/canceled': // User canceled the upload break; // ... case 'storage/unknown': // Unknown error occurred, inspect the server response break; } });
Web namespaced API
// Create a reference to the file we want to download var starsRef = storageRef.child('images/stars.jpg'); // Get the download URL starsRef.getDownloadURL() .then((url) => { // Insert url into an <img> tag to "download" }) .catch((error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { case 'storage/object-not-found': // File doesn't exist break; case 'storage/unauthorized': // User doesn't have permission to access the object break; case 'storage/canceled': // User canceled the upload break; // ... case 'storage/unknown': // Unknown error occurred, inspect the server response break; } });
همچنین میتوانید متادیتا را برای فایلهایی که در Cloud Storage ذخیره میشوند، دریافت یا بهروزرسانی کنید .