Node.js 如何使用Node列出google计算引擎存储的图像?
我试图使用NodeJS来列出谷歌计算引擎帐户可用的所有图像 我尝试使用Node以JSON格式获得与此命令行相同的结果:Node.js 如何使用Node列出google计算引擎存储的图像?,node.js,google-compute-engine,Node.js,Google Compute Engine,我试图使用NodeJS来列出谷歌计算引擎帐户可用的所有图像 我尝试使用Node以JSON格式获得与此命令行相同的结果: gcutil listimages --project=google 我从这段代码开始 我也研究了这个待办事项示例: 但是现在,我被卡住了,我不能真正地将示例与文档链接起来(请参阅代码中的链接,因为如果声誉
gcutil listimages --project=google
/*
Retrieves the list of image resources available to the specified project.
command line: gcutil listimages --project=google
REST API: https://developers.google.com/compute/docs/reference/latest/images/list
*/
"use strict";
var googleapis = require('googleapis');
var authclient = new googleapis.OAuth2Client();
var compute = new googleapis.auth.Compute();
var projectName = process.argv[2] || 'google';
var gceService;
var usage = 'usage: listimages.js <project name>';
compute.authorize(function (err, result) {
console.assert(!err, err);
googleapis.discover('compute', 'v1beta15')
.withAuthClient(compute)
.execute(function (err, client) {
console.assert(!err, err);
//here I want to instantiate a Google Compute Engine service
//this does not work (obviously)
gceService = client.instanciateGoogleComputeEngineService();
console.log('an instance of a GCE service is made !');
});
});
// the following line isn't part of the code, I just try to help you understand my needs :-)
console.log(gceService.listimages(projectName);
/*
检索指定项目可用的图像资源列表。
命令行:gcutil listimages--project=google
REST API:https://developers.google.com/compute/docs/reference/latest/images/list
*/
“严格使用”;
var googleapis=要求(“googleapis”);
var authclient=new googleapis.OAuth2Client();
var compute=new googleapis.auth.compute();
var projectName=process.argv[2]| |“谷歌”;
var-gceService;
var用法='用法:listimages.js';
compute.authorize(函数(错误、结果){
console.assert(!err,err);
googleapis.discover('compute','v1beta15')
.withAuthClient(计算)
.执行(功能(错误,客户端){
console.assert(!err,err);
//这里我想实例化一个Google计算引擎服务
//这不起作用(显然)
gceService=client.instanceCategoogleComputengineService();
log('生成GCE服务的实例!');
});
});
//下面这一行不是代码的一部分,我只是想帮助您理解我的需求:-)
console.log(gceService.listimages(projectName);
提前感谢您的帮助尽管谷歌计算引擎图像存储在谷歌云服务中,但您确实希望使用计算引擎API。请参阅 具体地 当我发送此
GET
命令时
GET https://www.googleapis.com/compute/v1beta16/projects/rti-latency/global/images?key={YOUR_API_KEY}
我看到结果了
200 OK
{
"kind": "compute#imageList",
"selfLink": "https://content.googleapis.com/compute/v1beta16/projects/<project-id>/global/images",
"id": "projects/<project-id>/global/images",
"items": [
{
"kind": "compute#image",
"selfLink": "https://content.googleapis.com/compute/v1beta16/projects/<project-id>/global/images/rti-5-debian-7-20140110",
"id": "17737537448393276537",
"creationTimestamp": "2014-01-09T14:25:56.614-08:00",
"name": "rti-5-debian-7-20140110",
"description": "",
"sourceType": "RAW",
"rawDisk": {
"containerType": "TAR",
"source": ""
},
"status": "READY",
"archiveSizeBytes": "2278217814"
}
]
}
200正常
{
“种类”:“计算#图像列表”,
“自链接”:https://content.googleapis.com/compute/v1beta16/projects//global/images",
“id”:“项目//全局/图像”,
“项目”:[
{
“种类”:“计算#图像”,
“自链接”:https://content.googleapis.com/compute/v1beta16/projects//global/images/rti-5-debian-7-20140110",
“id”:“17737537448393276537”,
“creationTimestamp”:“2014-01-09T14:25:56.614-08:00”,
“名称”:“rti-5-debian-7-20140110”,
“说明”:“,
“源类型”:“原始”,
“原始磁盘”:{
“containerType”:“TAR”,
“来源”:”
},
“状态”:“就绪”,
“archiveSizeBytes”:“2278217814”
}
]
}
你需要这样的东西
function listImages() {
var request = gapi.client.compute.images.list({
'project': DEFAULT_PROJECT,
'zone': DEFAULT_ZONE
});
executeRequest(request, 'listImages');
}
function executeRequest(request, apiRequestName) {
request.execute(function (resp) {
newWindow = window.open(apiRequestName, '', 'width=600, height=600, scrollbars=yes');
newWindow.document.write('<h1>' + apiRequestName + '</h1> <br />'
+ '<pre>' + JSON.stringify(resp.result, null, ' ') + '</pre>');
});
}
函数listImages(){
var request=gapi.client.compute.images.list({
“项目”:默认的_项目,
“区域”:默认的_区域
});
executeRequest(请求“listImages”);
}
函数executeRequest(请求,APIRestName){
请求执行(功能(resp){
newWindow=window.open(APIRestName,,'width=600,height=600,scrollbars=yes');
newWindow.document.write(“”+APIRestName+”
compute.request(opts, function(err, result) {
callback(err, result);
});
+''+JSON.stringify(resp.result,null'')+'';
compute.request(opts, function(err, result) {
callback(err, result);
});
});
}
如果有计算
变量设置,可以使用以下api查询api:
var gceImages = require("gce-images");
var images = gceImages({ keyFile: "..." });
images.getAll(function (err, images) {
// images contains a JSON with all the available images
});
其中,opts
应包括uri
,方法
和表单数据
,您可以使用NPM模块:
有关更多详细信息,请参阅