Cloud Storage 觸發條件


您可以觸發函式,以回應 Cloud Storage 中檔案和資料夾的上傳、更新或刪除作業。

本頁面的範例是以範例函式為基礎,該函式會在圖片檔案上傳至 Cloud Storage 時觸發。這個範例函式示範如何存取事件屬性、如何將檔案下載至 Cloud Functions 執行個體,以及處理 Cloud Storage 事件的其他基本概念。

如需更多用途示例,請參閱「Cloud Functions 可以做什麼?」一文。

Cloud Storage 變更時觸發函式

使用 functions.storage 建立處理 Cloud Storage 事件的函式。視您要將函式範圍限定在特定 Cloud Storage 值區,還是使用預設值區而定,請使用下列其中一個選項:

舉例來說,縮圖產生器範例的範圍是專案的預設值區:

exports.firstGenGenerateThumbnail = functions.storage.object().onFinalize(async (object) => {
  // ...
});

Cloud Storage 支援下列事件:

  • onArchive 只會在值區已啟用物件版本管理時傳送事件。這個事件表示物件的使用中版本已變為封存版本,原因是物件遭到封存或是因為擁有相同名稱的物件上傳而遭到覆寫。
  • onDelete 當永久刪除物件時,會傳送這個事件。這包含值區生命週期設定其中覆寫或刪除的物件。如果值區已啟用物件版本管理功能,則系統在封存物件時,不會傳送事件 (即使透過 storage.objects.delete 方法進行封存也不會傳送),詳情請參閱 onArchive 說明。
  • onFinalize 當在值區中成功建立新物件 (或現有物件的新版本) 時,會傳送這個事件。複製或重寫現有的物件也包含在內。上傳失敗不會觸發這個事件。
  • onMetadataUpdate 現有物件的中繼資料變更時,會傳送這個事件。

如上所示,在 on 事件處理常式中設定 onFinalize 的事件。

存取 Cloud Storage 物件屬性

Cloud Functions 會公開多個 Cloud Storage 物件屬性,例如 sizecontentType,用於更新檔案。每當物件的中繼資料有變更時,「metageneration」屬性就會遞增。如果是新物件,metageneration 值為 1

const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.

縮圖生成範例會使用部分屬性偵測函式傳回的結束案例:

// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
  return functions.logger.log('This is not an image.');
}

// Get the file name.
const fileName = path.basename(filePath);
// Exit if the image is already a thumbnail.
if (fileName.startsWith('thumb_')) {
  return functions.logger.log('Already a Thumbnail.');
}

下載、轉換及上傳檔案

在某些情況下,您可能不需要從 Cloud Storage 下載檔案。不過,如要執行密集型工作 (例如從 Cloud Storage 中儲存的檔案產生縮圖),您需要將檔案下載至函式執行個體,也就是執行程式碼的虛擬機器。

如要輕鬆下載物件並重新上傳至 Cloud Storage,請使用 npm install --save @google-cloud/storage 安裝 Google Cloud Storage 套件,然後匯入該套件。如要使用 JavaScript Promise 處理外部程序 (例如範例中的縮圖處理工作),請匯入 child-process-promise

const functions = require('firebase-functions/v1');
const admin = require('firebase-admin');
admin.initializeApp()
const path = require('path');

//library for resizing images
const sharp = require('sharp');

使用 gcs.bucket.file(filePath).download 將檔案下載到 Cloud Functions 執行個體的暫時目錄。您可以在這個位置視需要處理檔案,然後上傳至 Cloud Storage。執行非同步工作時,請務必在回呼中傳回 JavaScript Promise。

範例:圖片轉換

搭配使用 Cloud Functions 和圖片處理程式 (例如 sharp),即可對圖像檔案進行操作。以下範例說明如何為上傳的圖片檔案建立縮圖:

// Download file from bucket.
const bucket = admin.storage().bucket(fileBucket);
const metadata = {
  contentType: contentType,
};
const downloadResponse = await bucket.file(filePath).download();
const imageBuffer = downloadResponse[0];
functions.logger.log("Image downloaded!");

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

// Upload the thumbnail with a 'thumb_' prefix.
const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
await bucket.file(thumbFilePath).save(thumbnailBuffer, {
  metadata: metadata,
});
return functions.logger.log("Thumbnail uploaded!");

這段程式碼會為儲存在暫時目錄中的圖片建立 200x200 的縮圖,然後上傳回 Cloud Storage

查看更多範例

常見媒體轉換函式的更多範例,包括轉碼圖片審查內容擷取 EXIF 中繼資料。如需完整範例清單,請前往 GitHub。