我正在使用带有 HTTP 触发器的 Google Cloud Functions 来为 API 调用创建端点。此端点正在使用getSignedUrl
Cloud Storage 包中的函数。我想返回一个签名的 URL,以便客户端可以PUT
通过该 URL 上传文件。
我在使用此 API 时遇到了不一致的行为。有时我会从函数中得到一个 URL。然后我尝试更新存储桶名称,但出现此错误:
{ SigningError: Failure from metadata server.
at /user_code/node_modules/@google-cloud/storage/src/file.js:1715:16
at getCredentials (/user_code/node_modules/@google-cloud/storage/node_modules/google-auto-auth/index.js:264:9)
at googleAuthClient.getCredentials (/user_code/node_modules/@google-cloud/storage/node_modules/google-auto-auth/index.js:148:11)
at process._tickDomainCallback (internal/process/next_tick.js:135:7) message: 'Failure from metadata server.' }
Run Code Online (Sandbox Code Playgroud)
如果我改回存储桶名称,我仍然会看到此错误。我尝试了不同的方法,例如从终端部署。那给了我同样的错误。我还使用浏览器创建了该功能,并开始工作。在我尝试更新存储桶名称后,它停止工作。
通过谷歌搜索,我认为这是权限或 ACL 问题,但我不确定如何确认。文档说函数使用<YOUR_PROJECT_ID>@appspot.gserviceaccount.com
服务帐户运行,我可以在函数的常规选项卡中确认。我可以在 IAM 中看到此服务帐户具有Service Account Token Creator
,它说:
模拟服务帐户(创建 OAuth2 访问令牌、签署 blob 或 JWT 等)。
的文档getSignedUrl
说:
在这些环境中,我们调用 signBlob API 来创建签名 URL。该 API 需要https://www.googleapis.com/auth/iam或https://www.googleapis.com/auth/cloud-platform范围,因此请确保已启用它们。
我觉得这应该足以让它发挥作用。我不知道如何明确检查范围。 …