Elenca eventi del canale

Elenca tutti gli eventi del canale per una risorsa del canale in live streaming.

Per saperne di più

Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:

Esempio di codice

C#

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API C# per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.


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

public class ListChannelEventsSample
{
    public IList<Event> ListChannelEvents(
        string projectId, string regionId, string channelId)
    {
        // Create the client.
        LivestreamServiceClient client = LivestreamServiceClient.Create();

        ListEventsRequest request = new ListEventsRequest
        {
            ParentAsChannelName = ChannelName.FromProjectLocationChannel(projectId, regionId, channelId)
        };

        // Make the request.
        PagedEnumerable<ListEventsResponse, Event> response = client.ListEvents(request);

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

Go

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API Go per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

import (
	"context"
	"fmt"
	"io"

	"google.golang.org/api/iterator"

	livestream "cloud.google.com/go/video/livestream/apiv1"
	"cloud.google.com/go/video/livestream/apiv1/livestreampb"
)

// listChannelEvents lists all channel events for a given channel.
func listChannelEvents(w io.Writer, projectID, location, channelID string) error {
	// projectID := "my-project-id"
	// location := "us-central1"
	// channelID := "my-channel-id"
	ctx := context.Background()
	client, err := livestream.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	req := &livestreampb.ListEventsRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s/channels/%s", projectID, location, channelID),
	}

	it := client.ListEvents(ctx, req)
	fmt.Fprintln(w, "Channel events:")

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

Java

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API Java per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.


import com.google.cloud.video.livestream.v1.ChannelName;
import com.google.cloud.video.livestream.v1.Event;
import com.google.cloud.video.livestream.v1.ListEventsRequest;
import com.google.cloud.video.livestream.v1.LivestreamServiceClient;
import java.io.IOException;

public class ListChannelEvents {

  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";
    String channelId = "my-channel-id";

    listChannelEvents(projectId, location, channelId);
  }

  public static void listChannelEvents(String projectId, String location, String channelId)
      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. In this example, try-with-resources is used
    // which automatically calls close() on the client to clean up resources.
    try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) {
      var listEventsRequest =
          ListEventsRequest.newBuilder()
              .setParent(ChannelName.of(projectId, location, channelId).toString())
              .build();

      LivestreamServiceClient.ListEventsPagedResponse response =
          livestreamServiceClient.listEvents(listEventsRequest);
      System.out.println("Channel events:");

      for (Event event : response.iterateAll()) {
        System.out.println(event.getName());
      }
    }
  }
}

Node.js

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API Node.js per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

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

// Imports the Livestream library
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;

// Instantiates a client
const livestreamServiceClient = new LivestreamServiceClient();

async function listChannelEvents() {
  const iterable = await livestreamServiceClient.listEventsAsync({
    parent: livestreamServiceClient.channelPath(
      projectId,
      location,
      channelId
    ),
  });
  console.info('Channel events:');
  for await (const response of iterable) {
    console.log(response.name);
  }
}

listChannelEvents();

PHP

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API PHP per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

use Google\Cloud\Video\LiveStream\V1\Client\LivestreamServiceClient;
use Google\Cloud\Video\LiveStream\V1\ListEventsRequest;

/**
 * Lists the channel events for a given channel.
 *
 * @param string  $callingProjectId   The project ID to run the API call under
 * @param string  $location           The location of the channel
 * @param string  $channelId          The ID of the channel
 */
function list_channel_events(
    string $callingProjectId,
    string $location,
    string $channelId
): void {
    // Instantiate a client.
    $livestreamClient = new LivestreamServiceClient();
    $parent = $livestreamClient->channelName($callingProjectId, $location, $channelId);
    $request = (new ListEventsRequest())
        ->setParent($parent);

    $response = $livestreamClient->listEvents($request);
    // Print the channel list.
    $events = $response->iterateAllElements();
    print('Channel events:' . PHP_EOL);
    foreach ($events as $event) {
        printf('%s' . PHP_EOL, $event->getName());
    }
}

Python

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API Python per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.


import argparse

from google.cloud.video.live_stream_v1.services.livestream_service import (
    LivestreamServiceClient,
    pagers,
)

def list_channel_events(
    project_id: str, location: str, channel_id: str
) -> pagers.ListEventsPager:
    """Lists all events for a channel.
    Args:
        project_id: The GCP project ID.
        location: The location of the channel.
        channel_id: The user-defined channel ID."""

    client = LivestreamServiceClient()

    parent = f"projects/{project_id}/locations/{location}/channels/{channel_id}"
    page_result = client.list_events(parent=parent)
    print("Events:")

    responses = []
    for response in page_result:
        print(response.name)
        responses.append(response)

    return responses

Ruby

Per scoprire come installare e utilizzare la libreria client per l'API Live Stream, consulta la pagina dedicata alle librerie client dell'API Live Stream. Per maggiori informazioni, consulta la documentazione di riferimento dell'API Ruby per l'API Live Stream.

Per eseguire l'autenticazione all'API Live Stream, configura Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

require "google/cloud/video/live_stream"

##
# List the channel events
#
# @param project_id [String] Your Google Cloud project (e.g. "my-project")
# @param location [String] The location (e.g. "us-central1")
# @param channel_id [String] Your channel name (e.g. "my-channel")
#
def list_channel_events project_id:, location:, channel_id:
  # Create a Live Stream client.
  client = Google::Cloud::Video::LiveStream.livestream_service

  # Build the resource name of the parent.
  parent = client.channel_path project: project_id, location: location, channel: channel_id

  # Get the list of channel events.
  response = client.list_events parent: parent

  puts "Channel events:"
  # Print out all channel events.
  response.each do |event|
    puts event.name
  end
end

Passaggi successivi

Per cercare e filtrare esempi di codice per altri prodotti Google Cloud, consulta il browser di esempio Google Cloud.