简体   繁体   English

Google Cloud Functions - 如何从另一个项目的 GCP 存储库导入 Python package(通过 PIP)?

[英]Google Cloud Functions - How to import a Python package (via PIP) from a GCP Repository in another project?

I need to have private Python packages in GCP usable in multiple projects.我需要在 GCP 中有可用于多个项目的私有 Python 包。 I haven't tried the Artifact Registry since that's still in alpha, so right now I've been trying with simple repositories, but I'm open to alternatives.我没有尝试过 Artifact Registry,因为它仍处于 alpha 阶段,所以现在我一直在尝试使用简单的存储库,但我对替代方案持开放态度。

I have a Python package source code in a GCP Repository in Project A, and I have a cloud function in a repository also in Project A. In this cloud function I import the mentioned package by adding git+https://source.developers.google.com/p/project-a/r/my-python package in my requirements.txt file. I have a Python package source code in a GCP Repository in Project A, and I have a cloud function in a repository also in Project A. In this cloud function I import the mentioned package by adding git+https://source.developers.google.com/p/project-a/r/my-python package在我的requirements.txt文件中。

If I deploy this cloud function in Project A via gcloud functions in my terminal, specifying --source=https://source.developers.google.com/projects/project-a/repos/my-cloud-function and --project=project-a , it works fine, and the function can successfully import the elements from the package when I call it, but if I deploy this function in Project B instead, I get the following error:如果我通过终端中的gcloud functions在项目 A 中部署此云 function,请指定--source=https://source.developers.google.com/projects/project-a/repos/my-cloud-function--project=project-a ,它工作正常,当我调用它时,function 可以成功地从 package 导入元素,但是如果我在项目中部署此 ZC1C425268E68385D1AB5074C17A94F1,则会出现以下错误:

Deploying function (may take a while - up to 2 minutes)...failed.                                                                             
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: `pip_download_wheels` had stderr output:
  Running command git clone -q https://source.developers.google.com/p/project-a/r/my-python-package /tmp/pip-req-build-f_bcp4y9
  remote: PERMISSION_DENIED: The caller does not have permission
  remote: [type.googleapis.com/google.rpc.RequestInfo]
  remote: request_id: "abe4(...)"
  fatal: unable to access 'https://source.developers.google.com/p/project-a/r/my-python-package/': The requested URL returned error: 403
ERROR: Command errored out with exit status 128: git clone -q https://source.developers.google.com/p/project-a/r/my-python-package /tmp/pip-req-build-f_bcp4y9 Check the logs for full command output.

This seems like a permissions issue.这似乎是一个权限问题。 However, if I remove the package dependency from requirements.txt , it deploys fine, which means that Project B indeed has access to repos from Project A, so it seems like a issue inside Pip.但是,如果我从requirements.txt中删除 package 依赖项,它会正常部署,这意味着项目 B 确实可以访问项目 A 的存储库,所以这似乎是 Pip 内部的问题。 However, Pip has no problems if I deploy to Project A, so I'm a little lost.但是,如果我部署到项目 A,Pip 没有问题,所以我有点迷茫。

Many thanks in advance.提前谢谢了。

Artifact Registry is GA and no longer on Alpha/Beta since last year. Artifact Registry 是 GA ,自去年以来不再处于 Alpha/Beta 版。

I replicated your issue.我复制了你的问题。 The error is indeed due to permissions, it didn't happened on the deployment when you remove the line on the requirements.txt , probably because the credentials had access to both projects.该错误确实是由于权限引起的,当您删除requirements.txt上的行时,它没有发生在部署中,可能是因为凭据可以访问这两个项目。

In order to make the deployment correct you have to add the permissions on the repository to the service account that makes the deployment (which is the CF service account) that can be found on Cloud Functions - (select your Cloud Function) - Details , it should be something like project@appspot.gserviceaccount.com为了使部署正确,您必须将存储库的权限添加到进行部署的服务帐户(即 CF 服务帐户),该帐户可以在Cloud Functions - (select your Cloud Function) - Details上找到,它应该类似于project@appspot.gserviceaccount.com

Once you have located the service account add it to the Cloud Repository by clicking on Settings - Permissions and add it with at least the Source Repository Reader role找到服务帐户后,通过单击Settings - Permissions将其添加到云存储库,并至少使用Source Repository Reader角色添加它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCP Apache Airflow - 如何安装 Python ZEFE90A8E604A76C6840E88D0D 从私有存储库中导入 DAGB7? - GCP Apache Airflow - How to install Python package from a private repository and import on DAG? Google云功能 - 我可以将来自不同GCP项目仓库的代码部署到云功能吗? - Google Cloud Functions - Can I deploy code from a different GCP project repo into a Cloud Function? GCP - 云 Function 在同一项目中的工件注册表中找不到 Python package - GCP - Cloud Function can't find Python package from Artifact Registry in the same project Python:如何从 package 中导入 function,这取决于 ZEFE8B70A8E6034A787 中的其他功能? - Python: How to import a function from a package that depends on other functions in that package? 如何从另一个目录导入 python package? - How to Import python package from another directory? Python 和 HTTP Google Cloud Functions 上的“导入请求” - Python and "import requests" on HTTP Google Cloud Functions 从控制台或 console.cloud.google.com/functions/edit 部署 Python GCP 云函数时出现 INVALID_ARGUMENT 错误 - INVALID_ARGUMENT error when deploying Python GCP Cloud Function from either console or console.cloud.google.com/functions/edit Python google cloud function 部署失败 - Madmom Z62AD1C2A46C5298F3E2C95D3BAZBF8D0CZ ZEFE90A8E604A67C840E8D8F8 - Python google cloud function deployment failure - Madmom pip package 如何使用Python pip安装google.cloud? - How to install google.cloud with Python pip? Pip 在谷歌云应用引擎中从私有 github repo 安装 package - Pip install package from private github repo in google cloud appengine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM