使用出站流量和入站流量设置限制对服务的访问
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
允许仅由同一 Google Cloud 项目或 VPC Service Controls 服务边界中的资源(例如 Cloud Run)调用 Cloud Functions 函数,从而保护您的 Cloud Functions 函数。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Using egress and ingress settings to restrict access to services\n\nSecure your Cloud Functions by allowing them to be called only by resources, like Cloud Run, in the same Google Cloud project or VPC Service Controls service perimeter.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Configure network settings (1st gen)](/functions/1stgendocs/networking/network-settings)\n\nCode sample\n-----------\n\n### Python\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import os\n import urllib\n\n import google.auth.transport.requests\n import google.oauth2.id_token\n\n\n def get_hello_world(request):\n try:\n url = os.environ.get(\"URL\")\n req = urllib.request.Request(url)\n\n auth_req = google.auth.transport.requests.Request()\n id_token = google.oauth2.id_token.fetch_id_token(auth_req, url)\n req.add_header(\"Authorization\", f\"Bearer {id_token}\")\n\n response = urllib.request.urlopen(req)\n return response.read()\n\n except Exception as e:\n print(e)\n return str(e)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloudrun)."]]