List job templates

List all of the transcoding job templates for a given location.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

C#

Before trying this sample, follow the C# setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API C# API reference documentation.


using Google.Cloud.Video.Transcoder.V1;
using Google.Api.Gax.ResourceNames;
using Google.Api.Gax;
using System.Linq;
using System.Collections.Generic;

public class ListJobTemplatesSample
{
    public IList<JobTemplate> ListJobTemplates(string projectId, string location)
    {
        // Create the client.
        TranscoderServiceClient client = TranscoderServiceClient.Create();

        // Build the parent location name.
        LocationName parentLocation = new LocationName(projectId, location);

        // Call the API.
        PagedEnumerable<ListJobTemplatesResponse, JobTemplate> response = client.ListJobTemplates(parentLocation);

        // The returned sequence will lazily perform RPCs as it's being iterated over.
        return response.ToList();
    }
}

Go

Before trying this sample, follow the Go setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Go API reference documentation.

import (
	"context"
	"fmt"
	"io"

	"google.golang.org/api/iterator"

	transcoder "cloud.google.com/go/video/transcoder/apiv1"
	transcoderpb "google.golang.org/genproto/googleapis/cloud/video/transcoder/v1"
)

// listJobTemplates gets all previously-created job templates for a given
// location. See
// https://cloud.google.com/transcoder/docs/how-to/job-templates#list_job_template
// for more information.
func listJobTemplates(w io.Writer, projectID string, location string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	ctx := context.Background()
	client, err := transcoder.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %v", err)
	}
	defer client.Close()

	req := &transcoderpb.ListJobTemplatesRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
	}

	it := client.ListJobTemplates(ctx, req)
	fmt.Fprintln(w, "Job templates:")
	for {
		response, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListJobTemplates: %v", err)
		}
		fmt.Fprintln(w, response.GetName())
	}

	return nil
}

Java

Before trying this sample, follow the Java setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Java API reference documentation.


import com.google.cloud.video.transcoder.v1.JobTemplate;
import com.google.cloud.video.transcoder.v1.ListJobTemplatesRequest;
import com.google.cloud.video.transcoder.v1.LocationName;
import com.google.cloud.video.transcoder.v1.TranscoderServiceClient;
import java.io.IOException;

public class ListJobTemplates {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String location = "us-central1";

    listJobTemplates(projectId, location);
  }

  // Lists the job templates for a given location.
  public static void listJobTemplates(String projectId, String location) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (TranscoderServiceClient transcoderServiceClient = TranscoderServiceClient.create()) {

      var listJobTemplatesRequest =
          ListJobTemplatesRequest.newBuilder()
              .setParent(LocationName.of(projectId, location).toString())
              .build();

      // Send the list job templates request and process the response.
      TranscoderServiceClient.ListJobTemplatesPagedResponse response =
          transcoderServiceClient.listJobTemplates(listJobTemplatesRequest);
      System.out.println("Job templates:");

      for (JobTemplate jobTemplate : response.iterateAll()) {
        System.out.println(jobTemplate.getName());
      }
    }
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Node.js API reference documentation.

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// projectId = 'my-project-id';
// location = 'us-central1';

// Imports the Transcoder library
const {TranscoderServiceClient} =
  require('@google-cloud/video-transcoder').v1;

// Instantiates a client
const transcoderServiceClient = new TranscoderServiceClient();

async function listJobTemplates() {
  const [jobTemplates] = await transcoderServiceClient.listJobTemplates({
    parent: transcoderServiceClient.locationPath(projectId, location),
  });
  console.info('Job templates:');
  for (const jobTemplate of jobTemplates) {
    console.info(jobTemplate.name);
  }
}

listJobTemplates();

Python

Before trying this sample, follow the Python setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Python API reference documentation.


import argparse

from google.cloud.video.transcoder_v1.services.transcoder_service import (
    TranscoderServiceClient,
)


def list_job_templates(project_id, location):
    """Lists all job templates in a location.

    Args:
        project_id: The GCP project ID.
        location: The location of the templates."""

    client = TranscoderServiceClient()

    parent = f"projects/{project_id}/locations/{location}"
    response = client.list_job_templates(parent=parent)
    print("Job templates:")
    for jobTemplate in response.job_templates:
        print({jobTemplate.name})

    return response

Ruby

Before trying this sample, follow the Ruby setup instructions in the Transcoder API quickstart using client libraries. For more information, see the Transcoder API Ruby API reference documentation.

# project_id  = "YOUR-GOOGLE-CLOUD-PROJECT"  # (e.g. "my-project")
# location    = "YOUR-JOB-TEMPLATE-LOCATION"  # (e.g. "us-central1")

# Require the Transcoder client library.
require "google/cloud/video/transcoder"

# Create a Transcoder client.
client = Google::Cloud::Video::Transcoder.transcoder_service

# Build the resource name of the parent.
parent = client.location_path project: project_id, location: location

# Get the list of job templates.
response = client.list_job_templates parent: parent

puts "Job templates:"
# Print out all job templates.
response.each do |job_template|
  puts job_template.name.to_s
end

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.