resgen / lumen-gcp
v1.0.2
2020-03-25 18:01 UTC
Requires
- php: >=7.3
- laravel/lumen-framework: >=5.8
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-18 23:42:00 UTC
README
由环境变量驱动的 GCP 服务账户认证提供程序。从环境变量中 Base64 解码 Json 服务账户并将其作为可用的服务账户文件使用。同时支持在环境变量中使用多个 GCP 服务账户。
需求
- Lumen 5.8+
- 环境变量 APP_GCP_SERVICE_ACCOUNT,其中包含 Base64 编码的 GCP 服务账户 JSON
安装
使用 composer
composer require resgen/lumen-gcp:1.0.*
为什么是 Base64 Json?
当环境变量的值为 Base64 编码的值时,带有 JSON 值的环境变量更具系统通用性。某些系统可以很好地处理环境变量中的 JSON 值,而某些则不行。
Kubernetes
如果你正在使用 kubernetes secrets,请确保对值进行双重 Base64 编码。这将确保环境变量在 Pod 的环境变量内仍然被 Base64 编码。
基本示例
环境变量
# base64 service account json text. example is encoded {'foo':'bar'}
APP_GCP_SERVICE_ACCOUNT=e2ZvbzpiYXJ9
示例代码
use Google\Cloud\Storage\StorageClient; use Resgen\Common\Gcp\GoogleServiceAccountProvider; use Resgen\Common\Gcp\GoogleServiceAccount; // omitting Lumen app init...follow their examples // Register service account in your app $app->register(GoogleServiceAccountProvider::class); // Example GCP Storage client usage $gcpStorageClient = new StorageClient([ 'keyFilePath' => app(GoogleServiceAccount::class)->getFilePath() ]);
多个服务账户示例
环境变量
APP_GCP_ACCOUNTS=APP_GCP_SERVICE_ACCOUNT_ONE,APP_GCP_SERVICE_ACCOUNT_TWO,APP_GCP_SERVICE_ACCOUNT_THREE
# base64 service account json text. example is encoded {'foo':'bar'}
GCP_ACCOUNT_ONE=e2ZvbzpiYXJ9
GCP_ACCOUNT_TWO=e2ZvbzpiYXJ9
GCP_ACCOUNT_THREE=e2ZvbzpiYXJ9
示例代码
use Google\Cloud\Storage\StorageClient; use Resgen\Common\Gcp\GoogleServiceAccountProvider; use Resgen\Common\Gcp\GoogleServiceAccountBundle; // omitting Lumen app init...follow their examples // Register service account in your app $app->register(GoogleServiceAccountProvider::class); $gcpAccountBundle = app(GoogleServiceAccountBundle::class); // Example GCP Storage client usage $storageAccountOne = new StorageClient([ 'keyFilePath' => $gcpAccountBundle->get('GCP_ACCOUNT_ONE')->getFilePath() ]); $storageAccountTwo = new StorageClient([ 'keyFilePath' => $gcpAccountBundle->get('GCP_ACCOUNT_TWO')->getFilePath() ]); $storageAccountThree = new StorageClient([ 'keyFilePath' => $gcpAccountBundle->get('GCP_ACCOUNT_THREE')->getFilePath() ]);