![](/img/trans.png)
[英]Firebase Functions - How to return a document snapshot from a cloud function?
[英]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.