获取快照

此示例会检索有关快照的信息。

代码示例

Go

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

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

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
)

// getSnapshot prints a name of a disk snapshot in the specified project.
func getSnapshot(w io.Writer, projectID, snapshotName string) error {
	// projectID := "your_project_id"
	// snapshotName := "your_snapshot_name"

	ctx := context.Background()
	snapshotsClient, err := compute.NewSnapshotsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewSnapshotsRESTClient: %w", err)
	}
	defer snapshotsClient.Close()

	reqSnapshot := &computepb.GetSnapshotRequest{
		Project:  projectID,
		Snapshot: snapshotName,
	}

	snapshot, err := snapshotsClient.Get(ctx, reqSnapshot)
	if err != nil {
		return fmt.Errorf("unable to get snapshot: %w", err)
	}

	fmt.Fprintf(w, "Found snapshot: %s\n", snapshot.GetName())

	return nil
}

Java

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

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


package compute.disks;

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

public class GetSnapshot {

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

    // Name of the snapshot to look up.
    String snapshotName = "YOUR_SNAPSHOT_NAME";

    getSnapshot(projectId, snapshotName);
  }

  // Get information about a snapshot.
  public static void getSnapshot(String projectId, String snapshotName) 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()) {
      Snapshot snapshot = snapshotsClient.get(projectId, snapshotName);
      System.out.printf("Retrieved the snapshot: %s", snapshot.getName());
    }
  }
}

Python

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

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

from google.cloud import compute_v1


def get_snapshot(project_id: str, snapshot_name: str) -> compute_v1.Snapshot:
    """
    Get information about a Snapshot.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        snapshot_name: the name of the snapshot you want to look up.

    Returns:
        A Snapshot object.
    """

    snapshot_client = compute_v1.SnapshotsClient()

    return snapshot_client.get(project=project_id, snapshot=snapshot_name)

后续步骤

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