שימוש במטא-נתונים של קבצים ב-Cloud Storage באינטרנט
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
אחרי שמעלים קובץ לCloud Storage הפניה, אפשר גם לקבל או לעדכן את המטא-נתונים של הקובץ, למשל כדי לעדכן את סוג התוכן. בנוסף, בקבצים אפשר לאחסן צמדי מפתח/ערך בהתאמה אישית עם מטא-נתונים נוספים של הקובץ.
אחזור מטא-נתונים של קובץ
המטא-נתונים של הקובץ מכילים מאפיינים נפוצים כמו name, size ו-contentType (שנקרא לעיתים קרובות סוג MIME), בנוסף למאפיינים פחות נפוצים כמו contentDisposition ו-timeCreated. אפשר לאחזר את המטא-נתונים האלה מהפניה Cloud Storage באמצעות השיטה getMetadata(). הפונקציה getMetadata() מחזירה Promise שמכיל את המטא-נתונים המלאים, או שגיאה אם Promise דוחה את הבקשה.
Web
import{getStorage,ref,getMetadata}from"firebase/storage";// Create a reference to the file whose metadata we want to retrieveconststorage=getStorage();constforestRef=ref(storage,'images/forest.jpg');// Get metadata propertiesgetMetadata(forestRef).then((metadata)=>{// Metadata now contains the metadata for 'images/forest.jpg'}).catch((error)=>{// Uh-oh, an error occurred!});
// Create a reference to the file whose metadata we want to retrievevarforestRef=storageRef.child('images/forest.jpg');// Get metadata propertiesforestRef.getMetadata().then((metadata)=>{// Metadata now contains the metadata for 'images/forest.jpg'}).catch((error)=>{// Uh-oh, an error occurred!});
אפשר לעדכן את המטא-נתונים של הקובץ בכל שלב אחרי שהעלאת הקובץ מסתיימת באמצעות השיטה updateMetadata(). למידע נוסף על המאפיינים שאפשר לעדכן, אפשר לעיין ברשימה המלאה. רק המאפיינים שצוינו במטא-נתונים מתעדכנים,
כל השאר נשארים ללא שינוי. הפונקציה updateMetadata() מחזירה Promise עם המטא-נתונים המלאים, או שגיאה אם Promise דוחה את הבקשה.
Web
import{getStorage,ref,updateMetadata}from"firebase/storage";// Create a reference to the file whose metadata we want to changeconststorage=getStorage();constforestRef=ref(storage,'images/forest.jpg');// Create file metadata to updateconstnewMetadata={cacheControl:'public,max-age=300',contentType:'image/jpeg'};// Update metadata propertiesupdateMetadata(forestRef,newMetadata).then((metadata)=>{// Updated metadata for 'images/forest.jpg' is returned in the Promise}).catch((error)=>{// Uh-oh, an error occurred!});
// Create a reference to the file whose metadata we want to changevarforestRef=storageRef.child('images/forest.jpg');// Create file metadata to updatevarnewMetadata={cacheControl:'public,max-age=300',contentType:'image/jpeg'};// Update metadata propertiesforestRef.updateMetadata(newMetadata).then((metadata)=>{// Updated metadata for 'images/forest.jpg' is returned in the Promise}).catch((error)=>{// Uh-oh, an error occurred!});
import{getStorage,ref,updateMetadata}from"firebase/storage";conststorage=getStorage();constforestRef=ref(storage,'images/forest.jpg');// Create file metadata with property to deleteconstdeleteMetadata={contentType:null};// Delete the metadata propertyupdateMetadata(forestRef,deleteMetadata).then((metadata)=>{// metadata.contentType should be null}).catch((error)=>{// Uh-oh, an error occurred!});
// Create file metadata with property to deletevardeleteMetadata={contentType:null};// Delete the metadata propertyforestRef.updateMetadata(deleteMetadata).then((metadata)=>{// metadata.contentType should be null}).catch((error)=>{// Uh-oh, an error occurred!});
יכולות להיות כמה סיבות לשגיאות בקבלת מטא-נתונים או בעדכון שלהם, כולל קובץ שלא קיים או משתמש שאין לו הרשאה לגשת לקובץ הרצוי. מידע נוסף על שגיאות זמין בקטע Handle Errors במסמכים.
מטא-נתונים בהתאמה אישית
אפשר לציין מטא-נתונים בהתאמה אישית כאובייקט שמכיל מאפייני String.
Web
constmetadata={customMetadata:{'location':'Yosemite, CA, USA','activity':'Hiking'}};
אפשר להשתמש במטא-נתונים מותאמים אישית כדי לאחסן נתונים נוספים שספציפיים לאפליקציה עבור כל קובץ, אבל מומלץ מאוד להשתמש במסד נתונים (כמו Firebase Realtime Database) כדי לאחסן ולסנכרן את סוג הנתונים הזה.
מאפייני המטא-נתונים של הקובץ
בהמשך מופיעה רשימה מלאה של מאפייני המטא-נתונים בקובץ:
נכס
סוג
ניתן לכתיבה
bucket
מחרוזת
לא
generation
מחרוזת
לא
metageneration
מחרוזת
לא
fullPath
מחרוזת
לא
name
מחרוזת
לא
size
מספר
לא
timeCreated
מחרוזת
לא
updated
מחרוזת
לא
md5Hash
מחרוזת
YES בהעלאה, NO ב-updateMetadata
cacheControl
מחרוזת
כן
contentDisposition
מחרוזת
כן
contentEncoding
מחרוזת
כן
contentLanguage
מחרוזת
כן
contentType
מחרוזת
כן
customMetadata
אובייקט שמכיל מיפויים של מחרוזת->מחרוזת
כן
העלאה, הורדה ועדכון של קבצים הם חשובים, אבל חשוב גם להיות מסוגלים להסיר אותם. במאמר הזה נסביר איך למחוק קבצים מ-Cloud Storage.
[null,null,["עדכון אחרון: 2025-08-08 (שעון UTC)."],[],[],null,["\u003cbr /\u003e\n\nAfter uploading a file to Cloud Storage reference, you can also get\nor update the file metadata, for example to update the content type. Files\ncan also store custom key/value pairs with additional file metadata.\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nGet File Metadata\n\nFile metadata contains common properties such as `name`, `size`, and\n`contentType` (often referred to as MIME type) in addition to some less\ncommon ones like `contentDisposition` and `timeCreated`. This metadata can be\nretrieved from a Cloud Storage reference using\nthe `getMetadata()` method. `getMetadata()` returns a `Promise` containing the\ncomplete metadata, or an error if the `Promise` rejects. \n\nWeb \n\n```javascript\nimport { getStorage, ref, getMetadata } from \"firebase/storage\";\n\n// Create a reference to the file whose metadata we want to retrieve\nconst storage = getStorage();\nconst forestRef = ref(storage, 'images/forest.jpg');\n\n// Get metadata properties\ngetMetadata(forestRef)\n .then((metadata) =\u003e {\n // Metadata now contains the metadata for 'images/forest.jpg'\n })\n .catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_get_metadata.js#L8-L21\n```\n\nWeb \n\n```javascript\n// Create a reference to the file whose metadata we want to retrieve\nvar forestRef = storageRef.child('images/forest.jpg');\n\n// Get metadata properties\nforestRef.getMetadata()\n .then((metadata) =\u003e {\n // Metadata now contains the metadata for 'images/forest.jpg'\n })\n .catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L8-L18\n```\n\nUpdate File Metadata\n\nYou can update file metadata at any time after the file upload completes by\nusing the `updateMetadata()` method. Refer to the\n[full list](#file_metadata_properties) for more information on what properties\ncan be updated. Only the properties specified in the metadata are updated,\nall others are left unmodified. `updateMetadata()` returns a `Promise`\ncontaining the complete metadata, or an error if the `Promise` rejects. \n\nWeb \n\n```javascript\nimport { getStorage, ref, updateMetadata } from \"firebase/storage\";\n\n// Create a reference to the file whose metadata we want to change\nconst storage = getStorage();\nconst forestRef = ref(storage, 'images/forest.jpg');\n\n// Create file metadata to update\nconst newMetadata = {\n cacheControl: 'public,max-age=300',\n contentType: 'image/jpeg'\n};\n\n// Update metadata properties\nupdateMetadata(forestRef, newMetadata)\n .then((metadata) =\u003e {\n // Updated metadata for 'images/forest.jpg' is returned in the Promise\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_update_metadata.js#L8-L26\n```\n\nWeb \n\n```javascript\n// Create a reference to the file whose metadata we want to change\nvar forestRef = storageRef.child('images/forest.jpg');\n\n// Create file metadata to update\nvar newMetadata = {\n cacheControl: 'public,max-age=300',\n contentType: 'image/jpeg'\n};\n\n// Update metadata properties\nforestRef.updateMetadata(newMetadata)\n .then((metadata) =\u003e {\n // Updated metadata for 'images/forest.jpg' is returned in the Promise\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L26-L41\n```\n\nYou can delete a metadata property by setting it to `null`: \n\nWeb \n\n```javascript\nimport { getStorage, ref, updateMetadata } from \"firebase/storage\";\n\nconst storage = getStorage();\nconst forestRef = ref(storage, 'images/forest.jpg');\n\n// Create file metadata with property to delete\nconst deleteMetadata = {\n contentType: null\n};\n\n// Delete the metadata property\nupdateMetadata(forestRef, deleteMetadata)\n .then((metadata) =\u003e {\n // metadata.contentType should be null\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_delete_metadata.js#L8-L24\n```\n\nWeb \n\n```javascript\n// Create file metadata with property to delete\nvar deleteMetadata = {\n contentType: null\n};\n\n// Delete the metadata property\nforestRef.updateMetadata(deleteMetadata)\n .then((metadata) =\u003e {\n // metadata.contentType should be null\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L50-L62\n```\n\nHandle Errors\n\nThere are a number of reasons why errors may occur on getting or updating\nmetadata, including the file not existing, or the user not having permission\nto access the desired file. More information on errors can be found in the\n[Handle Errors](/docs/storage/web/handle-errors)\nsection of the docs.\n\nCustom Metadata\n\nYou can specify custom metadata as an object containing `String` properties. \n\nWeb \n\n```javascript\nconst metadata = {\n customMetadata: {\n 'location': 'Yosemite, CA, USA',\n 'activity': 'Hiking'\n }\n};https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_custom_metadata.js#L8-L13\n```\n\nWeb \n\n```javascript\nvar metadata = {\n customMetadata: {\n 'location': 'Yosemite, CA, USA',\n 'activity': 'Hiking'\n }\n};https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L68-L73\n```\n\nYou can use custom metadata for storing additional app specific data for each\nfile, but we highly recommend using a database (such as the\n[Firebase Realtime Database](/docs/database))\nto store and synchronize this type of data.\n\nFile Metadata Properties\n\nA full list of metadata properties on a file is available below:\n\n| Property | Type | Writable |\n|----------------------|--------------------------------------------|-------------------------------------|\n| `bucket` | string | NO |\n| `generation` | string | NO |\n| `metageneration` | string | NO |\n| `fullPath` | string | NO |\n| `name` | string | NO |\n| `size` | number | NO |\n| `timeCreated` | string | NO |\n| `updated` | string | NO |\n| `md5Hash` | string | YES on upload, NO on updateMetadata |\n| `cacheControl` | string | YES |\n| `contentDisposition` | string | YES |\n| `contentEncoding` | string | YES |\n| `contentLanguage` | string | YES |\n| `contentType` | string | YES |\n| `customMetadata` | Object containing string-\\\u003estring mappings | YES |\n\n| **Note:** at present, setting the `md5Hash` property on upload doesn't affect the upload, as hash verification is not yet implemented.\n\nUploading, downloading, and updating files is important, but so is being able\nto remove them. Let's learn how to\n[delete files](/docs/storage/web/delete-files)\nfrom Cloud Storage."]]