0

我正在 Symforny 5 中开发一个项目,我想使用 Google Cloud Storage,安装 phpleague / flysystem-bundle 并支持 superbalist / flysystem-google-storage,如文档中所述,在 Google Console 和存储桶中生成 credentials.json ,但我收到以下错误:

{
 "error": {
 "code": 401,
 "message": "Invalid Credentials",
 "errors": [
 {
  "message": "Invalid Credentials",
  "domain": "global",
  "reason": "authError",
  "locationType": "header",
  "location": "Authorization"
 }
 ]
}
}
\vendor\google\cloud-core\src\RequestWrapper.php (line 362)

配置:

flysystem:
storages:
    default.storage:
        adapter: 'local'
        options:
            directory: '%kernel.project_dir%/var/storage/default'

    gcs.storage:
        adapter: 'gcloud'
        options:
            client: 'gcloud_client_service' # The service ID of the Google\Cloud\Storage\StorageClient instance
            bucket: 'test-storage'
            #prefix: 'optional/path/prefix'
            api_url: 'https://storage.googleapis.com'

在 service.yml

gcloud_client_service:
    class: Google\Cloud\Storage\StorageClient
    arguments:
        - projectId: 'storage-project'
        - keyFilePath: '../credentials.json'
4

2 回答 2

1

您收到的错误是因为凭据设置不正确。如果您想使用 JSON 文件,解决此问题的一种方法是使用以下内容在代码中设置凭据:

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');

另一方面,在这个其他文档中,您可以找到另一种方法来设置与 GCS 的连接配置。还记得添加您要使用的服务帐户,转到IAM并将“存储桶管理员”角色添加到该 SA。

这两个选项都应该适合您。

于 2020-05-04T16:17:02.080 回答
0

有点晚了,但也许它可以帮助别人。今天我在 Symfony 5.1 上遇到了同样的问题,通过设置services.yaml解决了这个问题:

services:    
      gcloud_client_service:
        class: Google\Cloud\Storage\StorageClient
        arguments:
          $config:
            keyFilePath: '%kernel.project_dir%/config/my_testing_config.json'
            projectId: 'my-testing-project'
于 2021-04-14T09:38:58.037 回答