[英]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.