从App Engine获取计算元数据,未找到404页面错误

时间:2018-11-16 04:45:39

标签: google-app-engine google-cloud-platform google-compute-engine

我尝试从应用程序引擎获取项目范围的metadata,网址如下:

http://metadata.google.internal/computeMetadata/v1/project/attributes/IT_EBOOKS_API

stackdriver logging给我一个错误:

message: '404 - "404 page not found\\n"',

但是我可以从计算引擎中获得metadata。这是metadata的输出:

novaline_dulin@test:~$ curl http://metadata.google.internal/computeMetadata/v1/project/attributes/IT_EBOOKS_API -H 
"Metadata-Flavor: Google"
http://it-ebooks-api.info/v1novaline_dulin@test:~$ 

而且,这是我的代码,用于获取整个项目范围内的自定义metadata

const request = require('request-promise');

async function getMetaData(attr) {
  const url = `http://metadata.google.internal/computeMetadata/v1/project/attributes/${attr}`;
  const options = {
    headers: {
      'Metadata-Flavor': 'Google'
    }
  };
  console.log('url:', url);
  return request(url, options)
    .then((response) => {
      console.info(`Retrieve meta data successfully. meta data: ${response}`);
      return response;
    })
    .catch((err) => {
      console.error('Retrieve meta data failed.', err);
      return '';
    });
}

有什么问题吗?谢谢。

更新

我可以正确地从project-id服务器获得metadata。这是代码:

const METADATA_PROJECT_ID_URL = 'http://metadata.google.internal/computeMetadata/v1/project/project-id';

async function getProjectId() {
  const options = {
    headers: {
      'Metadata-Flavor': 'Google'
    }
  };

  return request(METADATA_PROJECT_ID_URL, options)
    .then((response) => {
      console.log('response: ', response);
      return response;
    })
    .catch((err) => {
      if (err && err.statusCode !== 200) {
        console.log('Error while talking to metadata server.');
        return 'Unknown_Project_ID';
      }
      return Promise.reject(err);
    });
}

3 个答案:

答案 0 :(得分:1)

前一阵子在标准环境中根本无法实现,请参见Is there a way to access the Google Cloud metadata service from AppEngine Standard for runtime configuration?

但是事情似乎正在改变。

(第1代)标准环境文档中有mentioning个元数据服务,

  • 仅适用于java沙箱
  • 可能的范围有限-实际上,所提及的端点的子集(可能是用户配置的)并没有包括在内。但是可能是一个解释问题(我的重点是):
  

下表列出了可以在其中创建HTTP的 个终结点   请求特定的元数据。

  • 只读:
  

注意:元数据访问权限目前是只读的:您无法为实例编写自己的元数据。

这意味着消除了DNS限制,该限制使不久前无法使用。由于您可以在灵活的环境中获取数据,这意味着它存在并且您并没有真正尝试编写它,因此您所体验到的也与只读限制无关。

看来,您寻求的服务功能/端点确实似乎更可能不可用/不起作用,至少对于go沙箱(如果不是所有沙箱)而言,而不是偶然的文档遗漏(一个人可能会怀疑/希望)。

答案 1 :(得分:1)

最后,我找到了原因,这是元数据API 版本问题。

代替使用

http://metadata.google.internal/computeMetadata/v1beta/project/attributes/${attr}

使用

http://metadata.google.internal/computeMetadata/v1/project/attributes/${attr}

现在,我可以从App Engine灵活的环境中获得metadata

{"IT_EBOOKS_API":"http://it-ebooks-api.info/v1","PROJECT_ID":"just-aloe-212502","API_KEY":"12j28flsrbapznq"}

但是对于GAE标准环境和GCF。仍然获得404 page not found

因此,我认为但不确定GCFGAE标准环境是否未在计算引擎中运行。

GAE灵活的环境使用计算引擎作为其基础结构。这就是为什么它可以从计算引擎获取元数据的原因。

答案 2 :(得分:0)

使用来自计算引擎VM的python API,我遇到了类似的错误。刷新密钥文件并使用GOOGLE_APPLICATION_CREDENTIALS环境变量来显式指向它似乎有效。即使该帐户是唯一配置了gcloud auth的帐户,我仍然遇到错误。我对Python库从何处获取凭据的理解可能会丢失一些东西,但至少目前我有一种解决方法。