Lab setup instructions and requirements
Protect your account and progress. Always use a private browser window and lab credentials to run this lab.

監控及管理 Google Cloud 資源:挑戰實驗室

Lab 20 minutes universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
This content is not yet optimized for mobile devices.
For the best experience, please visit us on a desktop computer using a link sent by email.

ARC101

Google Cloud 自學實驗室標誌

總覽

在挑戰研究室中,您會在特定情境下完成一系列任務。挑戰研究室不會提供逐步說明,您將運用從課程研究室學到的技巧,自行找出方法完成任務!自動評分系統 (如本頁所示) 將根據您是否正確完成任務來提供意見回饋。

在您完成任務的期間,挑戰研究室不會介紹新的 Google Cloud 概念。您須靈活運用所學技巧,例如變更預設值或詳讀並研究錯誤訊息,解決遇到的問題。

若想滿分達標,就必須在時限內成功完成所有任務!

設定

瞭解以下事項後,再點選「Start Lab」按鈕

請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。

您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。

為了順利完成這個實驗室,請先確認:

  • 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
  • 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。

挑戰情境

您剛獲聘,現在是初階雲端工程師,目前已協助多個團隊建立及管理 Google Cloud 資源。

公司認為您具備執行這些工作所需的技能與知識。

您的挑戰

現在,您必須協助新成立的開發團隊完成新專案的幾項初期工作;新專案名為「旅行」,目的是儲存及整理旅行相片。您要協助「旅行」團隊進行應用程式開發環境的初始設定。

您需要完成下列動作:

  • 建立 Cloud Storage bucket 並與第二位使用者共用。
  • 建立 Pub/Sub 主題,供 Cloud Run 函式傳送訊息。
  • 建立 Cloud Run 函式,每當在 Cloud Storage bucket 建立新物件時,就會執行這項函式。
  • 建立警告政策,當啟用中的 Cloud Run 函式執行個體數量達到特定門檻時,傳送通知電子郵件。

過程中請遵守下列規則:

  • 如未特別指定,所有資源均須建立於 區域。
  • 使用專案虛擬私有雲。
  • 名稱格式通常為「team-resource」,例如執行個體的名稱可以是「kraken-webserver1」。
  • 分配符合成本效益的資源大小。專案會受到監控。如果超量使用資源,該項資源所屬的專案就會終止,您的專案也有可能受到影響,因此請特別留意。監控團隊的指引如下:如未特別指定,小型 Linux VM 請使用 f1-micro,Windows 或 Kubernetes 節點等其他用途請使用 e2-medium

各項工作的詳細說明如下,祝您好運!

工作 1:建立 bucket

  1. 區域建立 bucket 並命名為 ,用於儲存相片。

  2. 在包含這個新 bucket 的專案中,將「Storage 物件檢視者」角色授予

點選「Check my progress」,確認目標已達成。 建立名為 的 bucket,並授予 存取權。

工作 2:建立 Pub/Sub 主題

  • 建立名為 的 Pub/Sub 主題,供 Cloud Run 函式傳送訊息。

點選「Check my progress」,確認目標已達成。 建立名為 的 Pub/Sub 主題。

工作 3:建立縮圖的 Cloud Run 函式

  1. 使用 Node.js 22 建立名為 的 Cloud Run 函式 (第 2 代)。每當工作 1 建立的 bucket 中有物件建立時,就會執行這個函式。

  2. 請務必將「進入點」 (要執行的函式) 設為 thumbnail,並將「觸發條件」設為 Cloud Storage

  3. index.js 第 15 行程式碼的 REPLACE_WITH_YOUR_TOPIC ID,換成您在工作 2 建立的

index.js:

/* globals exports, require */ //jshint strict: false //jshint esversion: 6 "use strict"; const crc32 = require("fast-crc32c"); const { Storage } = require('@google-cloud/storage'); const gcs = new Storage(); const { PubSub } = require('@google-cloud/pubsub'); const imagemagick = require("imagemagick-stream"); exports.thumbnail = (event, context) => { const fileName = event.name; const bucketName = event.bucket; const size = "64x64" const bucket = gcs.bucket(bucketName); const topicName = "REPLACE_WITH_YOUR_TOPIC ID"; const pubsub = new PubSub(); if ( fileName.search("64x64_thumbnail") == -1 ){ // doesn't have a thumbnail, get the filename extension var filename_split = fileName.split('.'); var filename_ext = filename_split[filename_split.length - 1]; var filename_without_ext = fileName.substring(0, fileName.length - filename_ext.length ); if (filename_ext.toLowerCase() == 'png' || filename_ext.toLowerCase() == 'jpg'){ // only support png and jpg at this point console.log(`Processing Original: gs://${bucketName}/${fileName}`); const gcsObject = bucket.file(fileName); let newFilename = filename_without_ext + size + '_thumbnail.' + filename_ext; let gcsNewObject = bucket.file(newFilename); let srcStream = gcsObject.createReadStream(); let dstStream = gcsNewObject.createWriteStream(); let resize = imagemagick().resize(size).quality(90); srcStream.pipe(resize).pipe(dstStream); return new Promise((resolve, reject) => { dstStream .on("error", (err) => { console.log(`Error: ${err}`); reject(err); }) .on("finish", () => { console.log(`Success: ${fileName} → ${newFilename}`); // set the content-type gcsNewObject.setMetadata( { contentType: 'image/'+ filename_ext.toLowerCase() }, function(err, apiResponse) {}); pubsub .topic(topicName) .publisher() .publish(Buffer.from(newFilename)) .then(messageId => { console.log(`Message ${messageId} published.`); }) .catch(err => { console.error('ERROR:', err); }); }); }); } else { console.log(`gs://${bucketName}/${fileName} is not an image I can handle`); } } else { console.log(`gs://${bucketName}/${fileName} already has a thumbnail`); } };

package.json:

{ "name": "thumbnails", "version": "1.0.0", "description": "Create Thumbnail of uploaded image", "scripts": { "start": "node index.js" }, "dependencies": { "@google-cloud/pubsub": "^2.0.0", "@google-cloud/storage": "^5.0.0", "fast-crc32c": "1.0.4", "imagemagick-stream": "4.1.1" }, "devDependencies": {}, "engines": { "node": ">=4.3.2" } } 注意: 您必須將一張 JPG 或 PNG 圖片上傳至 bucket,才能在成功建立函式後,驗證縮圖是否已建立。使用這張圖片 https://storage.googleapis.com/cloud-training/arc101/travel.jpg。將圖片下載至您的機器後,再上傳至 bucket。

點選「bucket 詳細資料」頁面中的「重新整理」按鈕,縮圖不久後就會出現。

上傳圖片檔後,就能點選下方來確認進度,不需等到縮圖建立完成。

注意:如果已順利部署函式,但 bucket 中仍未出現縮圖圖片,可以檢查「觸發條件」分頁,確認是否顯示您先前提供的函式觸發條件資訊。如果先前曾出現錯誤,可能沒有正確儲存。如果函式的「觸發條件」分頁沒有出現 Cloud Storage 觸發條件,您可以參閱「建立服務的觸發條件」說明文件頁面,重新建立觸發條件,再重新上傳新檔案來測試。新增檔案後,別忘了重新整理頁面。

點選「Check my progress」,確認目標已達成。 確認 Cloud Run 函式運作正常。

工作 4:建立警告政策

  1. 建立名為 Active Cloud Run Function Instances 的警告政策,當啟用中的 Cloud Run 函式執行個體數量大於零 (0) 時,系統會向您的個人電子郵件帳戶發出通知。

  2. 請務必選取「Cloud 函式」>「函式」>「啟用中的執行個體」做為指標。

點選「Check my progress」,確認目標已達成。 確認已建立 Cloud Run 函式警告。

恭喜!

「在 Google Cloud 監控及管理資料資源」徽章

取得下一枚技能徽章

這個自學實驗室是「監控及管理 Google Cloud 資源」技能徽章課程的一部分。完成這個技能徽章課程即可獲得上方的徽章,表彰您的成就。您可以在履歷表和社群平台張貼徽章,並加上 #GoogleCloudBadge 公開這項成就。

Google Cloud 教育訓練與認證

協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。

使用手冊上次更新日期:2025 年 3 月 10 日

實驗室上次測試日期:2025 年 3 月 10 日

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.