ทริกเกอร์ Cloud Storage


คุณสามารถทริกเกอร์ฟังก์ชันเพื่อตอบสนองต่อการอัปโหลด การอัปเดต หรือการลบไฟล์และโฟลเดอร์ใน Cloud Storage

ตัวอย่างในหน้านี้อิงตามฟังก์ชันตัวอย่างที่ทริกเกอร์เมื่อรูปภาพ อัปโหลดไฟล์ไปยัง Cloud Storage แล้ว ฟังก์ชันตัวอย่างนี้แสดงวิธีเข้าถึงแอตทริบิวต์เหตุการณ์ วิธีดาวน์โหลดไฟล์ไปยังอินสแตนซ์ Cloud Functions และพื้นฐานอื่นๆ ในการจัดการเหตุการณ์ Cloud Storage

นําเข้าโมดูลที่จําเป็น

ในการเริ่มต้นใช้งาน ให้นําเข้าโมดูลที่จําเป็นสําหรับการจัดการCloud Storageเหตุการณ์ ดังนี้

Node.js

 const {onObjectFinalized} = require("firebase-functions/v2/storage");

Python

 from firebase_functions import storage_fn

หากต้องการสร้างตัวอย่างแบบสมบูรณ์ ให้เพิ่มข้อกําหนดของ Firebase Admin SDK และเครื่องมือประมวลผลรูปภาพด้วย

Node.js

 const {initializeApp} = require("firebase-admin/app");
const {getStorage} = require("firebase-admin/storage");
const logger = require("firebase-functions/logger");
const path = require("path");

// library for image resizing
const sharp = require("sharp");

initializeApp();

Python

 import io
import pathlib

from PIL import Image

from firebase_admin import initialize_app

initialize_app()
from firebase_admin import storage

กำหนดขอบเขตฟังก์ชัน Cloud Storage

ใช้รูปแบบต่อไปนี้เพื่อกําหนดขอบเขตฟังก์ชันไปยังที่เก็บข้อมูล Cloud Storage ที่เฉพาะเจาะจง และตั้งค่าตัวเลือกที่ต้องการ

Node.js

// scope handler to a specific bucket, using storage options parameter
export archivedopts = onObjectArchived({ bucket: "myBucket" }, (event) => {
  //…
});

Python

# Scope handler to a specific bucket using storage options parameter
@storage_fn.on_object_archived(bucket="myBucket")
def archived_bucket(event: storage_fn.CloudEvent[storage_fn.StorageObjectData]):
    # ...

ในทางตรงกันข้าม ฟังก์ชันการสร้างภาพขนาดย่อตัวอย่างจะมีขอบเขตที่บัケットเริ่มต้นของโปรเจ็กต์

Node.js

exports.generateThumbnail = onObjectFinalized({cpu: 2}, async (event) => {
// ...
});

Python

@storage_fn.on_object_archived()
def generatethumbnail(event: storage_fn.CloudEvent[storage_fn.StorageObjectData]):
    # ...

ตั้งค่าตําแหน่งฟังก์ชัน

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

  • ตำแหน่งฟังก์ชันเหมือนกับตำแหน่งทริกเกอร์
  • ตำแหน่งของฟังก์ชันอยู่ภายในตำแหน่งทริกเกอร์ (เมื่อภูมิภาคทริกเกอร์เป็นภูมิภาคคู่/หลายภูมิภาค)
  • ฟังก์ชันอาจอยู่ในตําแหน่งใดก็ได้หากตั้งค่าภูมิภาคของทริกเกอร์เป็น us-central1

จัดการเหตุการณ์ Cloud Storage

ตัวแฮนเดิลเหล่านี้สําหรับการตอบสนองต่อเหตุการณ์ Cloud Storage พร้อมใช้งาน

Node.js

  • onObjectArchived ส่งเมื่อเปิดใช้ที่เก็บข้อมูลเท่านั้น การกำหนดเวอร์ชันออบเจ็กต์ เหตุการณ์นี้แสดงให้เห็นว่าเวอร์ชันที่ทำงานอยู่ของออบเจ็กต์ได้กลายเป็นเวอร์ชันที่เก็บถาวร เนื่องจากมีการเก็บถาวรหรือเขียนทับโดยการอัปโหลดออบเจ็กต์ที่มีชื่อเดียวกัน
  • onObjectDeleted ส่งเมื่อออบเจ็กต์ถูกลบอย่างถาวร ซึ่งรวมถึงออบเจ็กต์ที่ถูกเขียนทับหรือลบจากส่วนหนึ่งของการกำหนดค่าอายุการใช้งานของที่เก็บข้อมูล สำหรับที่เก็บข้อมูลที่เปิดใช้การกำหนดเวอร์ชันออบเจ็กต์ ระบบจะไม่ส่งเหตุการณ์นี้เมื่อมีการเก็บออบเจ็กต์ถาวร (ดู onArchive) แม้ว่าการเก็บถาวรจะเกิดขึ้นผ่านวิธีการ storage.objects.delete ก็ตาม
  • onObjectFinalized ส่งเมื่อมีการสร้างออบเจ็กต์ใหม่ (หรือรุ่นใหม่ของออบเจ็กต์ที่มีอยู่) ในที่เก็บข้อมูลเรียบร้อยแล้ว ซึ่งรวมถึงการคัดลอกหรือการเขียนออบเจ็กต์ที่มีอยู่ใหม่ การอัปโหลดที่ล้มเหลวจะไม่ทริกเกอร์เหตุการณ์นี้
  • onMetadataUpdated ส่งเมื่อมีการเปลี่ยนแปลงข้อมูลเมตาของออบเจ็กต์ที่มีอยู่

Python

  • on_object_archived ส่งเมื่อเปิดใช้ที่เก็บข้อมูลเท่านั้น การกำหนดเวอร์ชันออบเจ็กต์ เหตุการณ์นี้ระบุว่าเวอร์ชันที่เผยแพร่อยู่ของออบเจ็กต์ได้กลายเป็น เวอร์ชันที่เก็บถาวร เนื่องจากถูกเก็บถาวรหรือเป็นเพราะเวอร์ชันดังกล่าว ถูกเขียนทับโดยการอัปโหลดออบเจ็กต์ที่มีชื่อเดียวกัน
  • on_object_deleted ส่งเมื่อออบเจ็กต์ถูกลบอย่างถาวร ช่วงเวลานี้ มีออบเจ็กต์ที่ถูกเขียนทับหรือลบเป็นส่วนหนึ่งของที่เก็บข้อมูล การกำหนดค่าวงจร สำหรับที่เก็บข้อมูลที่เปิดใช้การกำหนดเวอร์ชันออบเจ็กต์ ระบบจะไม่ส่งเหตุการณ์นี้เมื่อมีการเก็บออบเจ็กต์ถาวร (ดู onArchive) แม้ว่าการเก็บถาวรจะเกิดขึ้นผ่านเมธอด storage.objects.delete ก็ตาม
  • on_object_finalized ส่งเมื่อออบเจ็กต์ใหม่ (หรือออบเจ็กต์ใหม่ ออบเจ็กต์ที่มีอยู่) สร้างในที่เก็บข้อมูลเรียบร้อยแล้ว ซึ่งรวมถึงการคัดลอกหรือการเขียนออบเจ็กต์ที่มีอยู่ใหม่ การอัปโหลดที่ล้มเหลวจะไม่ทริกเกอร์เหตุการณ์นี้
  • on_metadata_updated ส่งเมื่อมีการเปลี่ยนแปลงข้อมูลเมตาของออบเจ็กต์ที่มีอยู่

เข้าถึงแอตทริบิวต์ออบเจ็กต์ Cloud Storage

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

Node.js

const fileBucket = event.data.bucket; // Storage bucket containing the file.
const filePath = event.data.name; // File path in the bucket.
const contentType = event.data.contentType; // File content type.

Python

bucket_name = event.data.bucket
file_path = pathlib.PurePath(event.data.name)
content_type = event.data.content_type

ตัวอย่างการสร้างภาพขนาดย่อใช้แอตทริบิวต์เหล่านี้บางรายการเพื่อตรวจหาการออก ที่ฟังก์ชันแสดงผล:

Node.js

// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith("image/")) {
  return logger.log("This is not an image.");
}
// Exit if the image is already a thumbnail.
const fileName = path.basename(filePath);
if (fileName.startsWith("thumb_")) {
  return logger.log("Already a Thumbnail.");
}

Python

# Exit if this is triggered on a file that is not an image.
if not content_type or not content_type.startswith("image/"):
    print(f"This is not an image. ({content_type})")
    return

# Exit if the image is already a thumbnail.
if file_path.name.startswith("thumb_"):
    print("Already a thumbnail.")
    return

ดาวน์โหลด เปลี่ยนรูปแบบ และอัปโหลดไฟล์

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

การใช้ Cloud Functions ร่วมกับโปรแกรมประมวลผลรูปภาพ เช่น sharp สำหรับ Node.js และ Pillow สำหรับ Python ที่คุณทำได้ การปรับเปลี่ยนในไฟล์รูปภาพกราฟิก ต่อไปนี้เป็นตัวอย่างวิธีสร้างภาพปกสำหรับไฟล์ภาพที่อัปโหลด

Node.js

/**
 * When an image is uploaded in the Storage bucket,
 * generate a thumbnail automatically using sharp.
 */
exports.generateThumbnail = onObjectFinalized({cpu: 2}, async (event) => {

  const fileBucket = event.data.bucket; // Storage bucket containing the file.
  const filePath = event.data.name; // File path in the bucket.
  const contentType = event.data.contentType; // File content type.

  // Exit if this is triggered on a file that is not an image.
  if (!contentType.startsWith("image/")) {
    return logger.log("This is not an image.");
  }
  // Exit if the image is already a thumbnail.
  const fileName = path.basename(filePath);
  if (fileName.startsWith("thumb_")) {
    return logger.log("Already a Thumbnail.");
  }

  // Download file into memory from bucket.
  const bucket = getStorage().bucket(fileBucket);
  const downloadResponse = await bucket.file(filePath).download();
  const imageBuffer = downloadResponse[0];
  logger.log("Image downloaded!");

  // Generate a thumbnail using sharp.
  const thumbnailBuffer = await sharp(imageBuffer).resize({
    width: 200,
    height: 200,
    withoutEnlargement: true,
  }).toBuffer();
  logger.log("Thumbnail created");

  // Prefix 'thumb_' to file name.
  const thumbFileName = `thumb_${fileName}`;
  const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);

  // Upload the thumbnail.
  const metadata = {contentType: contentType};
  await bucket.file(thumbFilePath).save(thumbnailBuffer, {
    metadata: metadata,
  });
  return logger.log("Thumbnail uploaded!");
});

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

Python

@storage_fn.on_object_finalized()
def generatethumbnail(event: storage_fn.CloudEvent[storage_fn.StorageObjectData]):
    """When an image is uploaded in the Storage bucket, generate a thumbnail
    automatically using Pillow."""

    bucket_name = event.data.bucket
    file_path = pathlib.PurePath(event.data.name)
    content_type = event.data.content_type

    # Exit if this is triggered on a file that is not an image.
    if not content_type or not content_type.startswith("image/"):
        print(f"This is not an image. ({content_type})")
        return

    # Exit if the image is already a thumbnail.
    if file_path.name.startswith("thumb_"):
        print("Already a thumbnail.")
        return

    bucket = storage.bucket(bucket_name)

    image_blob = bucket.blob(str(file_path))
    image_bytes = image_blob.download_as_bytes()
    image = Image.open(io.BytesIO(image_bytes))

    image.thumbnail((200, 200))
    thumbnail_io = io.BytesIO()
    image.save(thumbnail_io, format="png")
    thumbnail_path = file_path.parent / pathlib.PurePath(f"thumb_{file_path.stem}.png")
    thumbnail_blob = bucket.blob(str(thumbnail_path))
    thumbnail_blob.upload_from_string(thumbnail_io.getvalue(), content_type="image/png")

โค้ดนี้จะสร้าง ภาพขนาดย่อ 200x200 สำหรับภาพที่บันทึกในไดเรกทอรีชั่วคราว แล้วอัปโหลด กลับไปที่ Cloud Storage