使用google cloud python api client创建磁盘快照

时间:2017-09-13 09:05:22

标签: python django google-cloud-platform google-compute-engine google-api-python-client

我尝试使用google云平台的python api客户端创建磁盘快照, 以下是我尝试过的内容:来自 views.py

        if request.method == 'POST':
        form = forms.SnapshotForm(request.POST)
        if form.is_valid():
            obj = snapy()
            obj.project = form.cleaned_data['project']
            obj.instance = form.cleaned_data['instance']
            obj.zone = form.cleaned_data['zone']
            obj.snapshot = form.cleaned_data['snap_name']
            # Google Cloud stuff for Snapshot
            service = discovery.build('compute', 'v1',
                                      credentials=credentials)
            project_id = obj.project
            zone = obj.zone
            disk = obj.instance
            snapshot_body = {}
            request = service.disks().createSnapshot(project=project_id, zone=zone,
                                                     disk=disk, body=snapshot_body)
            response = request.execute()
            pprint(response)
            obj.save()
            print(obj)
    return HttpResponse('Good', status=200)

它不返回任何错误,但未在gcp控制台上创建快照。 我配置有什么问题吗?请帮助我!

以下是回复:

  

[10 / Sep / 2017 08:09:46]" GET / snap / HTTP / 1.1" 200 2740

{'id': '8462873153659819689', 
 'insertTime': '2017-09-13T01:11:51.025-07:00',
 'kind': 'compute#operation',
 'name': 'operation-1505290310763-5590db641c1f8-9be2938e-9e1b974f',
 'operationType': 'createSnapshot',
 'progress': 0,
 'selfLink': 'https://www.googleapis.com/compute/v1/projects/istiodep/zones/asia-northeast1-c/operations/operation-1505290310763-5590db641c1f8-9be2938e-9e1b974f',
 'status': 'PENDING',
 'targetId': '1514253394633906452',
 'targetLink': 'https://www.googleapis.com/compute/v1/projects/istiodep/zones/asia-northeast1-c/disks/instance-1',
 'user': 'saqib.rasool@seecs.edu.pk',
 'zone': 'https://www.googleapis.com/compute/v1/projects/istiodep/zones/asia-northeast1-c'
}
Snapshot object

1 个答案:

答案 0 :(得分:0)

我认为,由于空的snapshot_body,您的问题可能会出现。

在google-cloud中,快照的名称是必填字段。 如果您使用service.zoneOperations检查操作状态,那么您可能会看到它的状态,您可能也会得到理由。

我能够使用以下代码段创建快照:

body = {
'name':'test-snap'
}

compute.disks().createSnapshot(
 project=project, zone=zone, body=body).execute()

如果这对您不起作用,请告诉我。