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在永久刪除物件時傳送。這包含值區生命週期設定其中覆寫或刪除的物件。如果值區已啟用物件版本管理,則在封存物件時,系統不會傳送事件 (請參閱 onArchive),即使透過 storage.objects.delete 方法進行封存也是如此。
  • onFinalize當在值區中成功建立新物件 (或現有物件的新版本) 時,會傳送這個事件。複製或重寫現有的物件也包含在內。上傳失敗不會觸發這個事件。
  • onMetadataUpdate 在現有物件的中繼資料變更時傳送。

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

存取 Cloud Storage 物件屬性

Cloud Functions 會顯示許多 Cloud Storage 物件屬性,例如 為 size敬上 和 contentType 更新該檔案。 「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,請安裝 Google Cloud Storage 套件 npm install --save @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 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 中繼資料完整清單如下: 。