Reputation: 51
I'm trying to download files from my bucket in Google Cloud Storage using PHP. I can upload well. I've tried some codes I've found here but Laravel tells me that class Google_Http_Request
is not found.
This is the code I'm trying:
$request = new Google_Http_Request($object['mediaLink'], 'GET');
$signed_request = $client->getAuth()->sign($request);
$http_request = $client->getIo()->makeRequest($signed_request);
echo $http_request->getResponseBody();
Upvotes: 0
Views: 5089
Reputation: 501
Here is an example of how to download an object using the latest version of the client library (v2.0):
// Authenticate your API Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
$storage = new Google_Service_Storage($client);
try {
// Google Cloud Storage API request to retrieve the list of objects in your project.
$object = $storage->objects->get($bucketName, $objectName);
} catch (Google_Service_Exception $e) {
// The bucket doesn't exist!
if ($e->getCode() == 404) {
exit(sprintf("Invalid bucket or object names (\"%s\", \"%s\")\n", $bucketName, $objectName));
}
throw $e;
}
// build the download URL
$uri = sprintf('https://storage.googleapis.com/%s/%s?alt=media&generation=%s', $bucketName, $objectName, $object->generation);
$http = $client->authorize();
$response = $http->get($uri);
if ($response->getStatusCode() != 200) {
exit('download failed!' . $response->getBody());
}
// write the file (or do whatever you'd like with it)
file_put_contents($downloadName, $response->getBody());
You can find other samples on using the storage API in this Samples Repository
There is also a newer library being developed called gcloud-php which has some documentation on calling Google Cloud Storage.
Upvotes: 3