Downloading files using google-api-php-client Downloading files using google-api-php-client json json

Downloading files using google-api-php-client


Old question, but it got me looking in the right direction. selfLink is the link to the metadata request, you need mediaLink to get the actual object data, and it's getAuth rather than getIo.

This script will output the file contents (given you have already initialised a $client object) :

$service = new Google_Service_Storage($client);$object = $service->objects->get('bucketname', 'objectname');$request = new Google_Http_Request($object->getMediaLink());$response = $client->getAuth()->authenticatedRequest($request);echo $response->getResponseBody();


This is not valid for apiclient ~2.0, see UPGRADING.md file in github.

Working code with apiclient ~2.0 should be:

$service = new Google_Service_Storage($client);$object = $service->objects->get('bucketname', 'objectname');// create an authorized HTTP client$httpClient = $client->authorize();$response = $httpClient->request('GET', $object->getMediaLink());echo $response->getBody();

or authorizing an existing Guzzle client:

$service = new Google_Service_Storage($client);$object = $service->objects->get('bucketname', 'objectname');// add authorization to an existing client$httpClient = new GuzzleHttp\Client();$httpClient = $client->authorize($httpClient);$response = $httpClient->request('GET', $object->getMediaLink());echo $response->getBody();


for downloading file, you need grant READER access to allUsers (you can do it from google web console or use google php api)