列出快照

此示例会列出项目中的所有快照。您可以通过指定过滤表达式来过滤结果。

代码示例

Go

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

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

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	"google.golang.org/api/iterator"
	computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
)

// listSnapshots prints a list of disk snapshots in the given project
func listSnapshots(w io.Writer, projectID, filter string) error {
	// projectID := "your_project_id"
	// filter := ""
	// Learn more about filters:
	// https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest
	ctx := context.Background()
	snapshotsClient, err := compute.NewSnapshotsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewSnapshotsRESTClient: %w", err)
	}
	defer snapshotsClient.Close()

	req := &computepb.ListSnapshotsRequest{
		Project: projectID,
		Filter:  &filter,
	}

	it := snapshotsClient.List(ctx, req)
	fmt.Fprintf(w, "Found snapshots:\n")
	for {
		snapshot, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return err
		}
		fmt.Fprintf(w, "- %s\n", snapshot.GetName())
	}
	return nil
}

Java

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

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


import com.google.cloud.compute.v1.ListSnapshotsRequest;
import com.google.cloud.compute.v1.Snapshot;
import com.google.cloud.compute.v1.SnapshotsClient;
import java.io.IOException;

public class ListSnapshots {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.

    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Filter to be applied when listing snapshots. Learn more about filters here:
    // https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest
    String filter = "FILTER_CONDITION";

    listSnapshots(projectId, filter);
  }

  // List snapshots from a project.
  public static void listSnapshots(String projectId, String filter) 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. After completing all of your requests, call
    // the `snapshotsClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (SnapshotsClient snapshotsClient = SnapshotsClient.create()) {

      // Create the List Snapshot request.
      ListSnapshotsRequest listSnapshotsRequest = ListSnapshotsRequest.newBuilder()
          .setProject(projectId)
          .setFilter(filter)
          .build();

      System.out.println("List of snapshots:");
      for (Snapshot snapshot : snapshotsClient.list(listSnapshotsRequest).iterateAll()) {
        System.out.println(snapshot.getName());
      }
    }
  }
}

Python

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

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

from __future__ import annotations

from collections.abc import Iterable

from google.cloud import compute_v1

def list_snapshots(project_id: str, filter_: str = "") -> Iterable[compute_v1.Snapshot]:
    """
    List snapshots from a project.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        filter_: filter to be applied when listing snapshots. Learn more about filters here:
            https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.types.ListSnapshotsRequest

    Returns:
        An iterable containing all Snapshots that match the provided filter.
    """

    snapshot_client = compute_v1.SnapshotsClient()
    request = compute_v1.ListSnapshotsRequest()
    request.project = project_id
    request.filter = filter_

    return snapshot_client.list(request)

后续步骤

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