[英]How to create disk from snapshot in google cloud function - node js
I have been struggling to find a solution for this particular problem. 我一直在努力寻找针对此特定问题的解决方案。 I've gone through almost all the documentation of
gcloud/compute
node
module which is used in google cloud functions
. 我已经遍历了
google cloud functions
使用的gcloud/compute
node
模块的几乎所有文档。
Now my challenge is to create a new disk
from an existing snapshot
in google cloud function
. 现在,我面临的挑战是从
google cloud function
的现有snapshot
创建新disk
。
I have used below code to create a disk. 我已使用以下代码创建磁盘。 As they haven't provided any example to create a
disk
from a snapshot
. 因为他们还没有提供任何从
snapshot
创建disk
示例。 Following cloud function
creates a new disk
named disk1
which is entirely fresh and new disk. 随后的
cloud function
创建一个名为disk1
的新disk
,该disk
完全是新磁盘。 I don't want that. 我不要 I want to create a disk from an existing
snapshot
which has some data and setup in it. 我想从现有
snapshot
创建一个磁盘,其中包含一些数据和设置。
exports.tempFunction = (req, res) => { // Example input: {"message": "Hello!"} const Compute = require(`@google-cloud/compute`); const compute = new Compute(); const zone = compute.zone('us-central1-a'); const disk = zone.disk('disk1'); const config = { // ... //os:'ubuntu' }; disk.create(config, function(err, disk, operation, apiResponse) { // `disk` is a Disk object. // `operation` is an Operation object that can be used to check the // status of the request. console.log(err); console.log(disk); console.log(operation); console.log(apiResponse); res.status(200).send("success"); }); };
Any help in this regard is highly appreciated. 在这方面的任何帮助都将受到高度赞赏。
PS I also tried using cloud APIs. PS我也尝试过使用云API。 But as I want only to use the
cloud functions
and I am unable to figure out that how do I get access token for gcloud
to use inside cloud functions
但是由于我只想使用
cloud functions
,所以我无法弄清楚如何获取gcloud
访问令牌以在cloud functions
内部使用
The disk creation [1] can be customized by setting the disk resource fields [2] in the config object. 可以通过在config对象中设置磁盘资源字段[2]来定制磁盘创建[1] 。 In this case, set the
sourceSnapshot
field in the config to the existing snapshot partial or full URL. 在这种情况下,请将配置中的
sourceSnapshot
字段设置为现有快照的部分或完整URL。 The code should look like this: 该代码应如下所示:
exports.tempFunction = (req, res) => { // Example input: {"message": "Hello!"} const Compute = require(`@google-cloud/compute`); const compute = new Compute(); const zone = compute.zone('us-central1-a'); const disk = zone.disk('disk1'); const config = { sourceSnapshot: "projects/{YOUR-PROJECT}/global/snapshots/{YOUR_SNAPSHOT}" }; disk.create(config, function(err, disk, operation, apiResponse) { // `disk` is a Disk object. // `operation` is an Operation object that can be used to check the // status of the request. console.log(err); console.log(disk); console.log(operation); console.log(apiResponse); res.status(200).send("success"); }); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.