python - Google Cloud Python 函数如何访问私有 Python 包
问题描述
- 我正在使用 Python 使用 Google Cloud Function。其他几个功能正在生产中。
- 但是,为此,我另外创建了一个自定义 Python 包,可在 github 上作为私有repo 使用。
- 我需要在 Google Function 中安装包
我做了什么
- 我使用在本地运行 Google 函数
functions-framework
- 我有一个 requirements.txt,其中有一个包的链接。这是通过将以下行添加到
requirements.txt
:
-e git+https://github.com/<repo>/<project>#egg=<project>
- 我跑
pip install -r requirements.txt
。并且该软件包已成功安装。 - 现在在函数的python代码中使用
import <pkg-name>
works,我可以访问所有函数。
将功能推送到云端时的挑战
- 根据文档,要将 Cloud 功能推送到 Google Cloud,我发出以下命令:
gcloud functions \
deploy <function-name> \
--trigger-http \
--entry-point <entry-function> \
--runtime python37 \
--project=<my-project>
正如预期的那样,这会产生错误,因为它无权访问 git 中的私有仓库。
我创建了一个谷歌云存储库并将其链接到 git 存储库,希望以某种方式我可以在 requirements.txt 中指定它。只是不知道如何。
我尝试在 Google Cloud Function 中为用户名和密码设置环境变量(这不是一个好主意,我同意),并将它们指定
requirements.txt
为:
-e git+https://${AUTH_USER}:${AUTH_PASSWORD}@github.com/<repo>/<project>#egg=<project>
这也给出了一个错误。
任何帮助或指导将不胜感激。
解决方案
虽然@marian.viadoi 在上述评论中是正确的,即。Google Cloud Function 无法访问私有 git 存储库,我已经实施了一种解决方法并共享以防万一。以下是已完成的工作:
- Python 包是根据(https://packaging.python.org/tutorials/packaging-projects/)中的文档准备的
- 使用创建二进制分发“whl”文件
python3 setup.py bdist_wheel
- 这个 whl 文件被放在了 google 函数文件夹中。我选择把它放在它的一个
dist
文件夹下。 - 在
requirements.txt
我添加了行:./dist/xxxx.whl
到云功能所需的其他依赖项 - 通过
gcloud deploy ..
- 成功安装后,包及其依赖项会自动安装在云功能的虚拟环境中
这没有回答如何在 Google Functions 中使用私有 repo(这是不可能的),但上述步骤确保私有包可以在不同的云功能之间轻松共享和更新。
推荐阅读
- angular - 在初始化离子 4 之前无法访问“LoginPageModule”
- node.js - 如何使用 sirv 访问 Heroku 分配的端口?
- ios - 使用委托 Swift 选择项目后 TableView 单元格未更新
- javascript - 统计/获取使用特定JS链接的网站链接
- reactjs - React Redux,对象作为 React 子级无效
- javascript - Highcharts:滚动显示 highcharts 容器内的文本区域;highcharts 容器内的自定义表格
- javascript - TypeScript - 结合两个接口并使其某些成员成为强制性的
- html - 如何在引导程序上使用模式创建多页表单
- java - 从角度 8 加密时,解密文本(AES 128 位)开头的垃圾值
- android - 为什么关闭选项菜单时不调用 onPanelCosed?