google/cloud-core
Shared core infrastructure for Google Cloud PHP libraries. Provides common utilities, configuration, retries, transport/auth helpers, and debugging support used across Google Cloud client components. Generally available with stable, backward-compatible releases.
Installation:
composer require google/cloud-core
Note: This package is not used directly—it’s a dependency for other Google Cloud PHP libraries (e.g., google/cloud-storage, google/cloud-bigquery). Install the relevant service package instead.
First Use Case: When integrating a Google Cloud service (e.g., Storage, BigQuery), the package handles:
GOOGLE_APPLICATION_CREDENTIALS or ADC (Application Default Credentials).Example (using google/cloud-storage):
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient(); // Leverages google/cloud-core for auth/retries
$bucket = $storage->bucket('my-bucket');
$objects = $bucket->objects(); // Uses core pagination/streaming
foreach ($objects as $object) {
echo $object->name();
}
Where to Look First:
google/cloud-core but documents its own API surface.Application Default Credentials (ADC): The package auto-detects credentials from:
GOOGLE_APPLICATION_CREDENTIALS (path to JSON keyfile).putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json');
$client = new Google\Cloud\Storage\StorageClient(); // Uses core auth logic
Service Account Keyfiles: Deprecated in favor of ADC, but legacy support exists via:
$client = new Google\Cloud\Storage\StorageClient([
'keyFile' => '/path/to/keyfile.json', // Deprecated (use ADC instead)
]);
Custom Credentials:
For non-Google environments, use the Google\Auth\Credentials class:
use Google\Auth\Credentials;
use Google\Auth\ServiceAccountCredentials;
$credentials = ServiceAccountCredentials::fromStream(file_get_contents('keyfile.json'));
$client = new Google\Cloud\Storage\StorageClient(['credentials' => $credentials]);
Automatic Retries:
Enabled by default for transient errors (e.g., 503 Service Unavailable, 429 Too Many Requests).
Customize via:
$client = new Google\Cloud\Storage\StorageClient([
'retry' => [
'max_attempts' => 5,
'timeout' => 30.0,
],
]);
Idempotency:
Use Google\Cloud\Core\Idempotency for retry-safe operations (e.g., uploads):
$uploader = $bucket->upload(
fopen('file.txt', 'r'),
['idempotency' => 'unique-upload-id']
);
Early Validation:
The OptionsValidator trait (added in v1.66.0) validates API constraints before requests:
$client->bucket('my-bucket')->object('file.txt')->update([
'metadata' => ['custom' => 'value'], // Validated against API schema
]);
Throws Google\Cloud\Core\Exceptions\InvalidArgumentException for invalid options.
Custom Validation:
Extend Google\Cloud\Core\ApiHelperTrait to add service-specific rules:
class MyClient extends \Google\Cloud\Core\Client {
use \Google\Cloud\Core\ApiHelperTrait;
protected function validateOptions(array $options) {
if (empty($options['required_param'])) {
throw new \InvalidArgumentException('Missing required parameter');
}
return parent::validateOptions($options);
}
}
Lazy-Loading Iterators:
Use Google\Cloud\Core\Iterator for efficient pagination:
foreach ($bucket->objects() as $object) { // Streams objects without loading all at once
echo $object->name();
}
Resumable Uploads:
For large files, use the core ResumableUploader:
$uploader = $bucket->upload(
fopen('large-file.zip', 'r'),
['resumable' => true]
);
Project ID Detection:
Use Google\Cloud\Core\DetectProjectIdTrait to auto-detect the project:
$client = new Google\Cloud\Storage\StorageClient();
$projectId = $client->detectProjectId(); // Falls back to metadata server
Emulator Support: Configure local emulators (e.g., Cloud Storage Emulator):
$client = new Google\Cloud\Storage\StorageClient([
'projectId' => 'my-project',
'emulatorHost' => 'http://localhost:4443', // Local emulator
]);
Debug Logging: Enable via environment variable:
export GOOGLE_CLOUD_DEBUG=1
Or programmatically:
putenv('GOOGLE_CLOUD_DEBUG=1');
$client = new Google\Cloud\Storage\StorageClient();
Structured Logging: Integrate with Laravel’s logging:
use Google\Cloud\Core\DebugLogger;
$logger = new DebugLogger(\Log::channel('google-cloud'));
$client = new Google\Cloud\Storage\StorageClient(['logger' => $logger]);
Direct Usage Anti-Pattern:
Google\Cloud\Core\Client directly. This package is a dependency, not an API surface.Google\Cloud\Storage\StorageClient).Deprecated keyFile Options:
keyFile and keyFilePath options are deprecated in favor of ADC. Migrate to:
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json');
RuntimeException: keyFile and keyFilePath are deprecated....Time Zone Handling:
DateTime objects to preserve timezone consistency. If you need timezone-aware operations, use:
$dt = new \DateTime('now', new \DateTimeZone('UTC'));
PHP 8.4+ Deprecations:
// Before (may fail in PHP 8.4+):
$value = $client->someMethod(); // Returns mixed|null
// After:
if ($value === null) { ... }
Emulator Without gRPC:
Retry Overrides:
// Risky: Overriding retries globally
$client = new Google\Cloud\Storage\StorageClient([
'retry' => ['max_attempts' => 1], // Disables retries entirely!
]);
Enable Debug Logging:
GOOGLE_CLOUD_DEBUG=1 to log HTTP requests/responses:
export GOOGLE_CLOUD_DEBUG=1
putenv('GOOGLE_CLOUD_DEBUG=1');
Inspect Requests:
Google\Cloud\Core\DebugLogger to capture raw requests:
$logger = new DebugLogger(function ($message) {
\Log::debug($message);
});
$client = new Google\Cloud\Storage\StorageClient(['logger' => $logger]);
Validate Credentials:
How can I help you explore Laravel packages today?