获取模板 (v1beta3)

检索存储在 Cloud Storage 中的 Dataflow 模板的元数据。

代码示例

Go

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


import (
	"context"
	"fmt"
	"io"

	dataflow "cloud.google.com/go/dataflow/apiv1beta3"
	dataflowpb "cloud.google.com/go/dataflow/apiv1beta3/dataflowpb"
)

// getTemplate retrieves metadata for a Cloud Dataflow template.
func getTemplate(ctx context.Context, w io.Writer, projectID string, locationID string, gcsPath string) error {
	client, err := dataflow.NewTemplatesClient(ctx)
	if err != nil {
		return fmt.Errorf("NewTemplatesClient failed: %w", err)
	}
	defer client.Close()

	req := &dataflowpb.GetTemplateRequest{
		ProjectId: projectID,
		Location:  locationID,
		Template: &dataflowpb.GetTemplateRequest_GcsPath{
			GcsPath: gcsPath,
		},
		View: dataflowpb.GetTemplateRequest_METADATA_ONLY,
	}

	resp, err := client.GetTemplate(ctx, req)
	if err != nil {
		return fmt.Errorf("client.GetTemplate: %w", err)
	}

	fmt.Fprintf(w, "Template name: %s\n", resp.GetMetadata().GetName())
	fmt.Fprintf(w, "Description: %s\n", resp.GetMetadata().GetDescription())

	return nil
}

Node.js

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

'use strict';

const {TemplatesServiceClient} = require('@google-cloud/dataflow');
const {status} = require('@grpc/grpc-js');

const client = new TemplatesServiceClient();

/**
 * Use the getTemplate method
 *
 * @param {string} projectId - The ID of the Google Cloud project that the job belongs to.
 * @param {string} locationId - The regional endpoint to which to direct the request (e.g., 'us-central1').
 * @param {string} gcsPath - A Cloud Storage path to the template to retrieve, beginning with 'gs://'.
 */
async function getTemplate(projectId, locationId, gcsPath) {
  try {
    const request = {
      projectId,
      location: locationId,
      gcsPath,
      view: 'METADATA_ONLY',
    };

    const [response] = await client.getTemplate(request);

    console.log(`Template type: ${response.templateType}`);
    if (response.metadata) {
      console.log(`Template name: ${response.metadata.name}`);
      console.log(`Template description: ${response.metadata.description}`);
    }

    return response;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Template not found at ${gcsPath}: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied accessing template: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument provided: ${err.message}`);
    } else {
      console.error(`API error retrieving template: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { getTemplate };

后续步骤

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