建立及執行範例工作
瞭解如何使用 Batch for Google Cloud建立及執行範例批次處理工作,以便轉碼影片。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Batch, Compute Engine, Logging and Cloud Storage APIs:
gcloud services enable batch.googleapis.com
compute.googleapis.com logging.googleapis.com storage.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Batch, Compute Engine, Logging and Cloud Storage APIs:
gcloud services enable batch.googleapis.com
compute.googleapis.com logging.googleapis.com storage.googleapis.com -
請確認您和作業的服務帳戶具備完成本教學課程所需的權限。本教學課程會使用作業的預設服務帳戶,也就是 Compute Engine 預設服務帳戶。
-
如要取得完成本教學課程所需的權限,請要求管理員授予下列 IAM 角色:
-
如要建立、查看及刪除工作:
-
批次工作編輯者 (
roles/batch.jobsEditor
) 專案 -
Compute Engine 預設服務帳戶的「服務帳戶使用者」 (
roles/iam.serviceAccountUser
)
-
批次工作編輯者 (
-
如要建立、查看及刪除 Cloud Storage 值區,您必須具備專案的「儲存空間管理員」 (
roles/storage.admin
) 權限。 -
如要查看作業的記錄,請在專案中點選「記錄檢視器」 (
roles/logging.viewer
)。
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
-
如要建立、查看及刪除工作:
-
為確保 Compute Engine 預設服務帳戶具備完成本教學課程所需的權限, 請管理員授予 Compute Engine 預設服務帳戶下列 IAM 角色:
-
批次工作代理程式回報者 (
roles/batch.agentReporter
) 專案 -
如要允許作業存取 Cloud Storage 值區:
專案的Storage Admin (
roles/storage.admin
) -
如要讓作業在 Cloud Logging 中產生記錄:
專案的「記錄檔寫入者」 (
roles/logging.logWriter
)
-
批次工作代理程式回報者 (
-
-
將
Batch Git 存放區複製到目前目錄:
git clone https://github.com/GoogleCloudPlatform/batch-samples.git
-
前往
transcoding
目錄:cd batch-samples/transcoding/
準備工作輸入內容
建立 Cloud Storage bucket:
gcloud storage buckets create gs://BUCKET_NAME
將
BUCKET_NAME
替換為值區的全域不重複名稱。輸出結果會與下列內容相似:
Creating gs://BUCKET_NAME/...
將
transcode.sh
指令碼和包含影片檔案的資料夾複製到 Cloud Storage bucket:gcloud storage cp -R transcode.sh input gs://BUCKET_NAME
輸出結果會與下列內容相似:
Copying file://transcode.sh to gs://BUCKET_NAME/transcode.sh Copying file://input/video-2.mp4 to gs://BUCKET_NAME/input/video-2.mp4 Copying file://input/video-1.mp4 to gs://BUCKET_NAME/input/video-1.mp4 Copying file://input/video-0.mp4 to gs://BUCKET_NAME/input/video-0.mp4 Completed files 4/4 | 37.5MiB/37.5MiB Average throughput: 48.4MiB/s
建立工作
在您選擇的文字編輯器中,開啟
job.json
設定檔。將
remotePath
欄位的值設為 Cloud Storage 值區名稱:{ "taskGroups": [ { "taskSpec": { "runnables": [ { "script": { "text": "bash /mnt/share/transcode.sh" } } ], "computeResource": { "cpuMilli": 2000, "memoryMib": 2048 }, "volumes": [ { "gcs": { "remotePath": "BUCKET_NAME" }, "mountPath": "/mnt/share" } ], "maxRetryCount": 2, "maxRunDuration": "600s" }, "taskCount": 3, "parallelism": 3 } ], "allocationPolicy": { "instances": [ { "policy": { "machineType": "n2d-standard-4", "provisioningModel": "SPOT" } } ] }, "labels": { "department": "creative", "env": "testing" }, "logsPolicy": { "destination": "CLOUD_LOGGING" } }
儲存變更並關閉文字編輯器。
建立
transcode
工作:gcloud batch jobs submit transcode \ --config=job.json \ --location=us-central1
輸出結果會與下列內容相似:
Job transcode-7a1654ca-211c-40e8-b0fb-8a00 was successfully submitted. ...
這項工作會並行執行 3 項任務。每項工作都會執行
transcode.sh
指令碼,分別為 3 個影片檔案中的 1 個檔案編碼,然後上傳至 Cloud Storage bucket。
監控工作
前往 Google Cloud 控制台的「Job list」(工作清單) 頁面。
在「Job name」(工作名稱) 欄中,按一下「transcode」(轉碼)。
「Job details」(工作詳細資料) 頁面隨即開啟。
按一下「事件」分頁標籤。
在「Events list」(事件清單) 專區中,您可以監控
transcode
工作的狀態。工作完成排入佇列、排程和執行所需的時間會因多項因素而異。在本例中,這項工作應該在約 5 分鐘內即可全部完成。選用:如要更新頁面,請按一下
「重新整理」。
在繼續進行下一步之前,請確認工作的狀態已設為「Succeeded」(成功)。如果工作失敗,請改為參閱「疑難排解」資訊。
查看經過編碼的影片
前往 Google Cloud 控制台的「Bucket」頁面。
在「Name」(名稱) 欄中,按一下 BUCKET_NAME。
「Bucket details」(值區詳細資料) 頁面隨即開啟。
在「Name」(名稱) 欄中,按一下
output/,然後點選其中一個編碼影片檔案。「物件詳細資料」頁面隨即開啟。
如要查看編碼後的影片,請依序點選「預覽」和「播放」圖示
。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。
刪除專案
如要避免付費,最簡單的方法就是刪除您在本教學課程中使用的專案。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
刪除個別資源
如要繼續使用目前的專案,請刪除本教學課程中使用的個別資源。
刪除工作
Batch 工作執行完畢後,請刪除 transcode
工作:
gcloud batch jobs delete transcode \
--location=us-central1
輸出結果會與下列內容相似:
Job projects/example-project/locations/us-central1/jobs/transcode deletion is in progress
刪除工作後,系統也會一併刪除工作詳細資料和記錄。 作業記錄會在Cloud Logging 記錄保留期限結束時自動刪除。
刪除值區
如果您不會用到本教學課程使用的 Cloud Storage 值區及其內容,請刪除該值區:
gcloud storage rm gs://BUCKET_NAME \
--recursive
輸出結果會與下列內容相似:
Removing objects:
Removing gs://BUCKET_NAME/input/video-0.mp4#1694788495332395...
Removing gs://BUCKET_NAME/input/video-2.mp4#1694788495296173...
Removing gs://BUCKET_NAME/input/video-1.mp4#1694788495228839...
Removing gs://BUCKET_NAME/output/video-0.mp4#1694788495332395...
Removing gs://BUCKET_NAME/output/video-2.mp4#1694788495296173...
Removing gs://BUCKET_NAME/output/video-1.mp4#1694788495228839...
Removing gs://BUCKET_NAME/transcode.sh#1694788495039427...
Completed 4/4
Removing Buckets:
Removing gs://BUCKET_NAME/...
Completed 1/1
刪除 Git 存放區
如果您不再需要為本教學課程複製的 Batch git 存放區,可以刪除該存放區:
cd ../../ && rm -rf batch-samples
後續步驟
瞭解如何開始使用 Batch。
進一步瞭解如何建立及執行工作。