Google/Cloud-Storage is a PHP client that facilitates world-wide storage and retrieval of any amount of data at any time. It is designed for a range of scenarios including serving website content, storing data for archival and disaster recovery, or distributing large data objects to users via direct download.
To use Google/Cloud-Storage, start by installing the dependency manager for PHP, Composer. Use the following command to install just the Google/Cloud-Storage component:
$ composer require google/cloud-storage
To install the complete suite, use:
$ composer require google/cloud
Authenticate your client as per the Authentication guide. Once authenticated, you can make requests.
Here are some sample codes showing how to use Google/Cloud-Storage:
Upload a file to the bucket:
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient();
$bucket = $storage->bucket('my_bucket');
$bucket->upload(
fopen('/data/file.txt', 'r')
);
Give read access to anyone with the URL while uploading a file:
$bucket->upload(
fopen('/data/file.txt', 'r'),
[
'predefinedAcl' => 'publicRead'
]
);
Download and store an object from the bucket locally:
$object = $bucket->object('file_backup.txt');
$object->downloadToFile('/data/file_backup.txt');
Use Google Cloud Storage as a stream wrapper:
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient();
$storage->registerStreamWrapper();
$contents = file_get_contents('gs://my_bucket/file_backup.txt');
The Google/Cloud-Storage documentation can be found at API documentation. You can get more in-depth usage samples at the official repository. Complement your understanding with official Google Cloud Storage documentation.