ใช้ข้อมูลเมตาของไฟล์กับ Cloud Storage บนเว็บ

หลังจากอัปโหลดไฟล์ไปยังข้อมูลอ้างอิง Cloud Storage แล้ว คุณยังรับหรืออัปเดตข้อมูลเมตาของไฟล์ได้ เช่น เพื่ออัปเดตประเภทเนื้อหา ไฟล์ยังสามารถจัดเก็บคู่คีย์/ค่าที่กำหนดเองพร้อมกับข้อมูลเมตาของไฟล์เพิ่มเติมได้

รับข้อมูลเมตาของไฟล์

ข้อมูลเมตาของไฟล์มีคุณสมบัติทั่วไป เช่น name size และ contentType (มักเรียกว่าประเภท MIME) นอกเหนือจากคุณสมบัติทั่วไปบางอย่าง เช่น contentDisposition และ timeCreated ข้อมูลเมตานี้สามารถดึงมาจากการอ้างอิง Cloud Storage ได้โดยใช้เมธอด getMetadata() getMetadata() ส่งคืน Promise ที่มีข้อมูลเมตาที่สมบูรณ์ หรือมีข้อผิดพลาดหาก Promise ปฏิเสธ

Web modular API

import { getStorage, ref, getMetadata } from "firebase/storage";

// Create a reference to the file whose metadata we want to retrieve
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Get metadata properties
getMetadata(forestRef)
  .then((metadata) => {
    // Metadata now contains the metadata for 'images/forest.jpg'
  })
  .catch((error) => {
    // Uh-oh, an error occurred!
  });

Web namespaced API

// Create a reference to the file whose metadata we want to retrieve
var forestRef = storageRef.child('images/forest.jpg');

// Get metadata properties
forestRef.getMetadata()
  .then((metadata) => {
    // Metadata now contains the metadata for 'images/forest.jpg'
  })
  .catch((error) => {
    // Uh-oh, an error occurred!
  });

อัปเดตข้อมูลเมตาของไฟล์

คุณสามารถอัปเดตข้อมูลเมตาของไฟล์ได้ตลอดเวลาหลังจากการอัปโหลดไฟล์เสร็จสิ้นโดยใช้เมธอด updateMetadata() ดู รายการทั้งหมด สำหรับข้อมูลเพิ่มเติมเกี่ยวกับคุณสมบัติที่สามารถอัปเดตได้ อัปเดตเฉพาะคุณสมบัติที่ระบุในข้อมูลเมตา ส่วนคุณสมบัติอื่นๆ ทั้งหมดไม่มีการแก้ไข updateMetadata() ส่งคืน Promise ที่มีข้อมูลเมตาที่สมบูรณ์ หรือมีข้อผิดพลาดหาก Promise ปฏิเสธ

Web modular API

import { getStorage, ref, updateMetadata } from "firebase/storage";

// Create a reference to the file whose metadata we want to change
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Create file metadata to update
const newMetadata = {
  cacheControl: 'public,max-age=300',
  contentType: 'image/jpeg'
};

// Update metadata properties
updateMetadata(forestRef, newMetadata)
  .then((metadata) => {
    // Updated metadata for 'images/forest.jpg' is returned in the Promise
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Web namespaced API

// Create a reference to the file whose metadata we want to change
var forestRef = storageRef.child('images/forest.jpg');

// Create file metadata to update
var newMetadata = {
  cacheControl: 'public,max-age=300',
  contentType: 'image/jpeg'
};

// Update metadata properties
forestRef.updateMetadata(newMetadata)
  .then((metadata) => {
    // Updated metadata for 'images/forest.jpg' is returned in the Promise
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

คุณสามารถลบคุณสมบัติข้อมูลเมตาได้โดยการตั้งค่าเป็น null :

Web modular API

import { getStorage, ref, updateMetadata } from "firebase/storage";

const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Create file metadata with property to delete
const deleteMetadata = {
  contentType: null
};

// Delete the metadata property
updateMetadata(forestRef, deleteMetadata)
  .then((metadata) => {
    // metadata.contentType should be null
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Web namespaced API

// Create file metadata with property to delete
var deleteMetadata = {
  contentType: null
};

// Delete the metadata property
forestRef.updateMetadata(deleteMetadata)
  .then((metadata) => {
    // metadata.contentType should be null
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

จัดการกับข้อผิดพลาด

มีสาเหตุหลายประการที่ทำให้เกิดข้อผิดพลาดในการรับหรืออัปเดตข้อมูลเมตา รวมถึงไฟล์ที่ไม่มีอยู่ หรือผู้ใช้ไม่มีสิทธิ์ในการเข้าถึงไฟล์ที่ต้องการ ข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดสามารถพบได้ในส่วน การจัดการข้อผิดพลาด ของเอกสาร

ข้อมูลเมตาที่กำหนดเอง

คุณสามารถระบุข้อมูลเมตาที่กำหนดเองเป็นออบเจ็กต์ที่มีคุณสมบัติ String

Web modular API

const metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

Web namespaced API

var metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

คุณสามารถใช้ข้อมูลเมตาที่กำหนดเองเพื่อจัดเก็บข้อมูลเฉพาะแอปเพิ่มเติมสำหรับแต่ละไฟล์ได้ แต่เราขอแนะนำอย่างยิ่งให้ใช้ฐานข้อมูล (เช่น ฐานข้อมูลเรียลไทม์ Firebase ) เพื่อจัดเก็บและซิงโครไนซ์ข้อมูลประเภทนี้

คุณสมบัติข้อมูลเมตาของไฟล์

รายการคุณสมบัติข้อมูลเมตาทั้งหมดในไฟล์มีอยู่ด้านล่าง:

คุณสมบัติ พิมพ์ เขียนได้
bucket เชือก เลขที่
generation เชือก เลขที่
metageneration เชือก เลขที่
fullPath เชือก เลขที่
name เชือก เลขที่
size ตัวเลข เลขที่
timeCreated เชือก เลขที่
updated เชือก เลขที่
md5Hash เชือก ใช่ในการอัปโหลด ไม่ใช่ใน updateMetadata
cacheControl เชือก ใช่
contentDisposition เชือก ใช่
contentEncoding เชือก ใช่
contentLanguage เชือก ใช่
contentType เชือก ใช่
customMetadata วัตถุที่มีการแมปสตริง -> การแมปสตริง ใช่

การอัปโหลด ดาวน์โหลด และอัปเดตไฟล์เป็นสิ่งสำคัญ แต่ก็สามารถลบไฟล์เหล่านั้นได้ด้วยเช่นกัน มาเรียนรู้วิธีการ ลบไฟล์ออก จาก Cloud Storage กันดีกว่า