GCP Transcoder API用preset/web-hd建作业时添加PubSub Destination
问题背景
- 基于GCP Transcoder API处理签名URL上传至Cloud Storage的视频转码,已配置存储桶写入触发的Cloud Function,核心转码流程运行正常,需要新增转码完成后的通知推送能力。
- 参考社区MP4转HLS方案编写基于预设创建转码作业的代码,尝试配置PubsubDestination指向指定Pub/Sub主题接收转码完成通知,但对应配置片段始终无法生效。
- 已查阅Transcoder API Pub/Sub配置官方文档及社区相关解答,未找到可行解决方案。
原实现代码
def create_job_from_preset(project_id, location, input_uri, output_uri, preset): """Creates a job based on a job preset. Args: project_id: The GCP project ID. location: The location to start the job in. input_uri: Uri of the video in the Cloud Storage bucket. output_uri: Uri of the video output folder in the Cloud Storage bucket. preset: The preset template (for example, 'preset/web-hd').""" client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" job = transcoder_v1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.template_id = preset job.ttl_after_completion_days = 1 job.config = transcoder_v1.types.JobConfig( PubsubDestination={ topic_name=f"projects/{project_id}/topics/testing" } ) response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
失效配置片段
job.config = transcoder_v1.types.JobConfig( PubsubDestination={ topic_name=f"projects/{project_id}/topics/testing" } )
失效原因
配置不生效有三个核心问题:
- 字段使用错误:
JobConfig中配置Pub/Sub目标的字段为蛇形命名的pubsub_destinations,类型是PubsubDestination对象列表,不是直接传入PubsubDestination字典参数;且PubsubDestination对象中指定主题的字段为topic,不是topic_name。 - 配置逻辑冲突:使用
template_id指定预设模板创建作业时,直接给job.config赋值会完全覆盖预设模板内置的所有转码规则,不仅通知配置无法生效,转码作业本身也会因为缺少必要的转码参数运行异常。 - 权限未配置:Transcoder服务账号默认没有指定Pub/Sub主题的发布权限,就算参数配置正确也无法推送通知。
修复方案
- 先拉取指定预设模板的完整配置,在模板配置基础上追加Pub/Sub通知配置,避免覆盖原有转码规则。
- 使用正确的字段名和参数类型传入Pub/Sub配置。
- 给Transcoder服务账号绑定目标Pub/Sub主题的发布权限。
修复后的可运行代码:
def create_job_from_preset(project_id, project_number, location, input_uri, output_uri, preset): """Creates a job based on a job preset. Args: project_id: The GCP project ID. project_number: The GCP project number. location: The location to start the job in. input_uri: Uri of the video in the Cloud Storage bucket. output_uri: Uri of the video output folder in the Cloud Storage bucket. preset: The preset template (for example, 'preset/web-hd').""" client = TranscoderServiceClient() parent = f"projects/{project_id}/locations/{location}" # 获取预设模板的完整配置,自定义预设直接替换模板名称拼接逻辑即可 preset_id = preset.replace("preset/", "") template_name = f"projects/{project_id}/locations/{location}/jobTemplates/{preset_id}" template_config = client.get_job_template(name=template_name).config job = transcoder_v1.types.Job() job.input_uri = input_uri job.output_uri = output_uri job.ttl_after_completion_days = 1 # 基于模板配置追加Pub/Sub通知,不覆盖原有转码规则 job.config = template_config pubsub_target = transcoder_v1.types.PubsubDestination( topic=f"projects/{project_id}/topics/testing" ) job.config.pubsub_destinations.append(pubsub_target) response = client.create_job(parent=parent, job=job) print(f"Job: {response.name}") return response
权限配置说明
提前给Transcoder官方服务账号service-{project_number}@gcp-sa-transcoder.iam.gserviceaccount.com绑定目标主题的pubsub.publisher角色,否则通知会推送失败。
内容的提问来源于stack exchange,提问作者Mrcreamio
相关产品推荐
相关产品推荐

