如何在 Google Cloud 中查找已部署的 Python 函数的当前项目 id 出现错误

问题描述 投票:0回答:3

我已在 Google Cloud 中部署了 Python 3.7 函数。我需要通过代码获取project-id来找出它部署在哪里。

我编写了一个小的Python 3.7脚本并通过Google shell命令行进行测试

import urllib
import urllib.request
url="http://metadata.google.internal/computeMetadata/v1/project/project-id"
x=urllib.request.urlopen(url)
with x as response:
 x.read()

不幸的是,这只能给我

b''
作为回应。尽管我已经使用

设置了项目ID,但我没有得到项目ID
gcloud config set project my-project

我是 Google Cloud 和 Python 新手。

第2期

这是下面的附加问题:

在我的本地系统中,我已经安装了 gcloud,如果我从那里运行上面的 python3.7 脚本:

x=urllib.request.urlopen(url) #this line

我从上面的行中得到了这个异常:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1350, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1240, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1286, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1235, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1006, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 946, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 917, in connect
    self.sock = self._create_connection(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

答案所建议的以下更改也给了我空字符串:

python python-requests google-api-python-client google-cloud-python
3个回答
15
投票

在Python 3.7运行时,您可以通过环境变量获取项目ID

import os
project_id = os.environ['GCP_PROJECT']

在未来的运行时,此环境变量将不可用,您需要从元数据服务器获取项目ID:

import urllib.request
url = "http://metadata.google.internal/computeMetadata/v1/project/project-id"
req = urllib.request.Request(url)
req.add_header("Metadata-Flavor", "Google")
project_id = urllib.request.urlopen(req).read().decode()

在本地运行此命令将产生错误,因为您的本地计算机无法解析

http://metadata.google.internal
URL - 这仅适用于已部署的函数。


3
投票

在 Cloud Shell 中,DEVSHELL_PROJECT_ID 环境变量包含您的项目 ID。

因此,您可以运行以下命令来使用Python获取您的项目ID。

import os
USER = os.getenv('DEVSHELL_PROJECT_ID')
print(USER)

如果您想在云功能中获取项目ID,您可以从包含project_id的服务帐户credentials.json中获取它:

# If this is running in a cloud function, then GCP_PROJECT should be defined
if 'GCP_PROJECT' in os.environ:
    project_id = os.environ['GCP_PROJECT']
    
# else if this is running locally then GOOGLE_APPLICATION_CREDENTIALS should be defined
elif 'GOOGLE_APPLICATION_CREDENTIALS' in os.environ:
    with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS'], 'r') as fp:
        credentials = json.load(fp)
    project_id = credentials['project_id']
else:
    raise Exception('Failed to determine project_id')

0
投票

Google 发布了 Go 的官方元数据库,可以在 cloud.google.com/go/compute/metadata 找到。要获取项目 ID,您只需使用 ProjectID() 函数即可。如需了解更多详情,请访问 https://pkg.go.dev/cloud.google.com/go/compute/metadata#ProjectID

© www.soinside.com 2019 - 2024. All rights reserved.