列出作业

列出给定位置的所有转码作业。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

C#

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 C# 设置说明进行操作。 如需了解详情,请参阅 Transcoder API C# API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


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

public class ListJobsSample
{
    public IList<Job> ListJobs(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<ListJobsResponse, Job> response = client.ListJobs(parentLocation);

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

Go

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Go 设置说明进行操作。 如需了解详情,请参阅 Transcoder API Go API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	"google.golang.org/api/iterator"

	transcoder "cloud.google.com/go/video/transcoder/apiv1"
	"cloud.google.com/go/video/transcoder/apiv1/transcoderpb"
)

// listJobs lists all jobs for a given location. See
// https://cloud.google.com/transcoder/docs/how-to/jobs#list_jobs for more
// information.
func listJobs(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: %w", err)
	}
	defer client.Close()

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

	it := client.ListJobs(ctx, req)
	fmt.Fprintln(w, "Jobs:")

	for {
		response, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListJobs: %w", err)
		}
		fmt.Fprintln(w, response.GetName())
	}
	return nil
}

Java

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Java 设置说明进行操作。 如需了解详情,请参阅 Transcoder API Java API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.cloud.video.transcoder.v1.Job;
import com.google.cloud.video.transcoder.v1.ListJobsRequest;
import com.google.cloud.video.transcoder.v1.LocationName;
import com.google.cloud.video.transcoder.v1.TranscoderServiceClient;
import java.io.IOException;

public class ListJobs {

  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";

    listJobs(projectId, location);
  }

  // Lists the jobs for a given location.
  public static void listJobs(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()) {

      ListJobsRequest listJobsRequest =
          ListJobsRequest.newBuilder()
              .setParent(LocationName.of(projectId, location).toString())
              .build();

      // Send the list jobs request and process the response.
      TranscoderServiceClient.ListJobsPagedResponse response =
          transcoderServiceClient.listJobs(listJobsRequest);
      System.out.println("Jobs:");

      for (Job job : response.iterateAll()) {
        System.out.println(job.getName());
      }
    }
  }
}

Node.js

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 Transcoder API Node.js API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

/**
 * 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 listJobs() {
  const iterable = await transcoderServiceClient.listJobsAsync({
    parent: transcoderServiceClient.locationPath(projectId, location),
  });
  console.info('Jobs:');
  for await (const response of iterable) {
    console.log(response.name);
  }
}

listJobs();

PHP

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 PHP 设置说明进行操作。 如需了解详情,请参阅 Transcoder API PHP API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

use Google\Cloud\Video\Transcoder\V1\Client\TranscoderServiceClient;
use Google\Cloud\Video\Transcoder\V1\ListJobsRequest;

/**
 * Lists all Transcoder jobs in a location.
 *
 * @param string $projectId The ID of your Google Cloud Platform project.
 * @param string $location The location of the jobs.
 */
function list_jobs($projectId, $location)
{
    // Instantiate a client.
    $transcoderServiceClient = new TranscoderServiceClient();

    $formattedParent = $transcoderServiceClient->locationName($projectId, $location);
    $request = (new ListJobsRequest())
        ->setParent($formattedParent);
    $response = $transcoderServiceClient->listJobs($request);

    // Print job list.
    $jobs = $response->iterateAllElements();
    print('Jobs:' . PHP_EOL);
    foreach ($jobs as $job) {
        printf('%s' . PHP_EOL, $job->getName());
    }
}

Python

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 Transcoder API Python API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


import argparse

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

def list_jobs(
    project_id: str,
    location: str,
) -> pagers.ListJobsPager:
    """Lists all jobs in a location.

    Args:
        project_id: The GCP project ID.
        location: The location of the jobs.

    Returns:
        An iterable object containing job resources.
    """

    client = TranscoderServiceClient()

    parent = f"projects/{project_id}/locations/{location}"
    response = client.list_jobs(parent=parent)
    print("Jobs:")
    for job in response.jobs:
        print({job.name})

    return response

Ruby

在试用此示例之前,请按照 Transcoder API 快速入门:使用客户端库中的 Ruby 设置说明进行操作。 如需了解详情,请参阅 Transcoder API Ruby API 参考文档

如需向 Transcoder API 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

# project_id = "YOUR-GOOGLE-CLOUD-PROJECT"  # (e.g. "my-project")
# location   = "YOUR-JOB-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 jobs.
response = client.list_jobs parent: parent

puts "Jobs:"
# Print out all jobs.
response.each do |job|
  puts job.name
end

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器