簡體   English   中英

如何從Google Cloud Function中的快照創建磁盤-Node.js

[英]How to create disk from snapshot in google cloud function - node js

我一直在努力尋找針對此特定問題的解決方案。 我已經遍歷了google cloud functions使用的gcloud/compute node模塊的幾乎所有文檔。

現在,我面臨的挑戰是從google cloud function的現有snapshot創建新disk

我已使用以下代碼創建磁盤。 因為他們還沒有提供任何從snapshot創建disk示例。 隨后的cloud function創建一個名為disk1的新disk ,該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"); }); }; 

在這方面的任何幫助都將受到高度贊賞。

PS我也嘗試過使用雲API。 但是由於我只想使用cloud functions ,所以我無法弄清楚如何獲取gcloud訪問令牌以在cloud functions內部使用

可以通過在config對象中設置磁盤資源字段[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"); }); }; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM