创建并运行示例作业
了解如何使用 Batch for Google Cloud创建和运行示例批处理作业,以对视频进行转码。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 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. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 
  
  
    Create or select a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 - 
        Create a Google Cloud project: gcloud projects create PROJECT_ID Replace PROJECT_IDwith 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_IDwith your Google Cloud project name.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Batch, Compute Engine, Logging and Cloud Storage APIs: Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable batch.googleapis.com compute.googleapis.com logging.googleapis.com storage.googleapis.com 
- 
      Install the Google Cloud CLI. 
- 
          如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。 
- 
        如需初始化 gcloud CLI,请运行以下命令: gcloud init
- 
  
  
    Create or select a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 - 
        Create a Google Cloud project: gcloud projects create PROJECT_ID Replace PROJECT_IDwith 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_IDwith your Google Cloud project name.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Batch, Compute Engine, Logging and Cloud Storage APIs: Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable batch.googleapis.com compute.googleapis.com logging.googleapis.com storage.googleapis.com 
- 
请确保您和作业的服务账号拥有完成本教程所需的权限。本教程使用作业的默认服务账号,即 Compute Engine 默认服务账号。 - 
  
  
  
  
  
  
  
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  如需获得完成本教程所需的权限,请让您的管理员为您授予以下 IAM 角色: - 
            如需创建、查看和删除作业,请执行以下操作:
            - 
  
  
    
      项目的 Batch Job Editor  (roles/batch.jobsEditor)
- 
  
  
    
      Compute Engine 默认服务账号的 Service Account User  (roles/iam.serviceAccountUser)
 
- 
  
  
    
      项目的 Batch Job Editor  (
- 
            如需创建、查看和删除 Cloud Storage 存储分区:
              
  
  
    
      项目的 Storage Admin  (roles/storage.admin)
- 
            如需查看作业的日志,请执行以下操作:
              
  
  
    
      Logs Viewer  (roles/logging.viewer) 在项目中
 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。 
- 
            如需创建、查看和删除作业,请执行以下操作:
            
- 
  
  
  
  
  
  
  
  
  
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  为确保 Compute Engine 默认服务账号具有完成本教程所需的权限,请让您的管理员为 Compute Engine 默认服务账号授予以下 IAM 角色: - 
  
  
    
      项目的 Batch Agent Reporter  (roles/batch.agentReporter)
- 
            如需允许作业访问 Cloud Storage 存储分区:
              
  
  
    
      项目的 Storage Admin  (roles/storage.admin)
- 
            如需让作业在 Logging 中生成日志:
              
  
  
    
      项目的 Logs Writer  (roles/logging.logWriter)
 
- 
  
  
    
      项目的 Batch Agent Reporter  (
 
- 
  
  
  
  
  
  
  
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
  
    
    
    
    
    
    
      
      
        
        
        
        
        
      
    
    
    
    
    
  
  
- 
将 
Batch Git 代码库克隆到当前目录:git clone https://github.com/GoogleCloudPlatform/batch-samples.git 
- 
转到 transcoding目录:cd batch-samples/transcoding/ 
- 创建 Cloud Storage 存储分区,请运行以下命令: - gcloud storage buckets create gs://BUCKET_NAME- 将 - BUCKET_NAME替换为存储桶的全局唯一名称。- 输出类似于以下内容: - Creating gs://BUCKET_NAME/...
- 将 - transcode.sh脚本和包含视频文件的文件夹复制到您的 Cloud Storage 存储桶:- 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 个视频文件中的一个进行编码,并将其上传到 Cloud Storage 存储桶。
- 在 Google Cloud 控制台中,前往作业列表页面。 
- 在作业名称列中,点击 transcode。 - 作业详情页面随即打开。 
- 点击活动标签页。 - 在事件列表部分中,您可以监控 - transcode作业的状态。作业完成排队、调度和运行所需的时间因多种因素而异。在本例中,作业预计会在大约 5 分钟内全部完成。
- 可选:如需更新页面,请点击 刷新。 
- 在 Google Cloud 控制台中,前往存储桶页面。 
- 在名称列中,点击 BUCKET_NAME。 - 系统会打开存储分区详情页面。 
- 在名称列中,点击 output/,然后点击某个编码的视频文件。 - 对象详情页面随即会打开。 
- 如需查看编码后的视频,请点击预览,然后点击 播放。 
- 了解如何开始使用 Batch。 
- 详细了解如何创建和执行作业。 
准备作业输入
创建作业
监控作业
在继续执行下一步之前,请确保作业的状态已设为成功。如果作业失败,请改为参阅问题排查。
查看经过编码的视频
清理
为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。
删除项目
避免支付费用的最简单方法是删除您在本教程中使用的项目。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
删除各个资源
如果您想继续使用当前项目,请删除本教程中使用的各个资源。
删除作业
在批处理作业运行完毕后,删除 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