GCP Google_Client API:如何使用名称创建快照?

时间:2016-02-29 17:07:33

标签: google-api google-compute-engine google-cloud-platform google-api-php-client

https://github.com/google/google-api-php-client

我在谷歌计算引擎(GCE)上使用google-api-php-client来创建快照。我发现如何获取和删除快照但我没有找到如何创建它。因此,我现在正在使用gcloud命令,如下所示。

public function backup() {

  try {
      $client = new \Google_Client();
      $client->useApplicationDefaultCredentials();
      $client->setScopes(['https://www.googleapis.com/auth/compute']);

      $compute_service = new \Google_Service_Compute($client);
      try {
          $compute_service->snapshots->get('project-name', 'snapshot-name', array());
          $compute_service->snapshots->delete('project-name', 'snapshot-name', array());
      } catch (\Exception $e) {}

      exec("gcloud compute disks snapshot instance-name --snapshot-names snapshot-name --zone xxxx");

  } catch (\Exception $e) {
      Log::error($e);
  }

}

我找到了“createSnapshot”mehtod,但它没有“name”属性,我不知道什么是“Google_Service_Compute_Snapshot”属性。

public function createSnapshot($project, $zone, $disk, Google_Service_Compute_Snapshot $postBody, $optParams = array())

您能告诉我如何使用Google_Client创建快照吗?

1 个答案:

答案 0 :(得分:3)

$snapshot = new \Google_Service_Compute_Snapshot();
$snapshot->setName('snapshot-name');
$compute_service->disks->createSnapshot('project-name', 'asia-east1-a', 'instance-name', $snapshot, array());