【问题标题】:How to create disk from snapshot in google cloud function - node js如何在谷歌云功能中从快照创建磁盘-node js
【发布时间】:2018-05-09 11:52:17
【问题描述】:

我一直在努力寻找解决这个特定问题的方法。我几乎浏览了gcloud/compute node 模块的所有文档,该模块在google cloud functions 中使用。

现在我的挑战是从google cloud function 中的现有snapshot 创建一个新的disk

我使用下面的代码来创建磁盘。因为他们没有提供任何示例来从snapshot 创建disk。在cloud function 之后创建一个名为disk1 的新disk,它是全新的磁盘。我不想要那个。我想从现有的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");
  });
 
};

非常感谢您在这方面的任何帮助。

附:我也尝试过使用云 API。但由于我只想使用cloud functions,我无法弄清楚如何获取gcloud 的访问令牌以在cloud functions 内部使用

【问题讨论】:

    标签: javascript node.js google-cloud-platform google-cloud-functions


    【解决方案1】:

    可以通过在配置对象中设置磁盘资源字段[2]来自定义磁盘创建[1]。 在这种情况下,请将配置中的 sourceSnapshot 字段设置为现有的快照部分或完整 URL。代码应如下所示:

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

    【讨论】:

    • 感谢您的回复。实际上,我最终使用 API 来实现这一点,因为 config 对象中的设置参数没有给我任何结果。我确实尝试过使用sourceSnapshot 参数。但我使用的是快照的名称而不是路径。我会用路径再试一次,让你知道会发生什么。
    • 您好@DhrumiPatel,您有机会使用路径测试sourceSnapshot 解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2019-08-15
    • 2021-08-30
    相关资源
    最近更新 更多