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 承諾處理外部流程,例如 並匯入 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。時間 執行非同步工作時,請務必在 回呼。

範例:圖片轉換

您可以將 Cloud Functionssharp 等圖片處理程式搭配使用,對圖像圖片檔案執行操作。以下範例說明如何 為上傳的圖片檔案建立縮圖圖片:

// 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!");

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

查看更多範例

其他常見的媒體轉換函式範例,包括 圖像轉碼 管理內容 擷取 EXIF 中繼資料完整清單如下: 。