我可以将JSON身份验证文件与带有php的Cloud Firestore客户端一起使用吗



我正在测试云Firestore php客户端库(https://github.com/googleapis/google-cloud-php/tree/master/Firestore(

在云存储客户端中,此代码有效:

require 'vendor/autoload.php';
use GoogleCloudStorageStorageClient;
// Authenticating with keyfile data.
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);
// Authenticating with a keyfile path.
$storage = new StorageClient([
'keyFilePath' => '/path/to/keyfile.json'
]);
// Providing the Google Cloud project ID.
$storage = new StorageClient([
'projectId' => 'myProject'
]);

我想知道是否可以使用相同的代码来初始化Firestore客户端?尤其是和杰森的那部分。

它与projetctId配合使用很好,但如果可能的话,我想将它与keyFile或keyFilePath配合使用。

感谢您提供任何线索。

对于PHP库和其他语言来说,更好、更实用的是使用环境变量,而不是在代码中声明JSON密钥路径和项目

GOOGLE_CLOUD_PROJECT - name of the project
GOOGLE_APPLICATION_CREDENTIALS - Path to JSON file

这种方法保证了代码与一些无服务器GCP产品(如云功能和应用程序引擎(的兼容性。

此外,这是一个很好的做法,因为如果你将此代码上传到存储库,证书将不会包含在你的代码中

要在firestore客户端的代码上声明您的凭据,请检查以下代码:

$db = new FirestoreClient([
'projectId' => $projectId,
'keyFilePath' =>  '/path/to/keyfile.json',
]);

Firestore文档中没有提到这一点,但在类Firestoreclient的源代码中显示了可以用于初始化客户端的所有选项

@type string $apiEndpoint A hostname with optional port to use in
place of the service's default endpoint.
@type string $projectId The project ID from the Google Developer's
Console.
@type CacheItemPoolInterface $authCache A cache for storing access
tokens. **Defaults to** a simple in memory implementation.
@type array $authCacheOptions Cache configuration options.
@type callable $authHttpHandler A handler used to deliver Psr7
requests specifically for authentication.
@type callable $httpHandler A handler used to deliver Psr7 requests.
Only valid for requests sent over REST.
@type array $keyFile The contents of the service account credentials
.json file retrieved from the Google Developer's Console.
Ex: `json_decode(file_get_contents($path), true)`.
@type string $keyFilePath The full path to your service account
credentials .json file retrieved from the Google Developers
Console.
@type int $retries Number of retries for a failed request. **Defaults
to** `3`.
@type array $scopes Scopes to be used for the request.
@type bool $returnInt64AsObject If true, 64 bit integers will be
returned as a {@see GoogleCloudCoreInt64} object for 32 bit
platform compatibility. **Defaults to** false.

相关内容

最新更新