Reputation: 276
Is there a way to download a file from Google Cloud Storage in PHP,instead of just reading it and save the content to a file using php functions ?
My code is this:
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file);
$storage = new Google_Service_Storage($this->gcsClient);
$object = $storage->objects->get($this->bucket, $file);
$request = new Google_Http_Request($object['mediaLink'], 'GET');
$signed_request = $this->gcsClient->getAuth()->sign($request);
$http_request = $this->gcsClient->getIo()->makeRequest($signed_request);
$options = ['gs' => ['Content-Type' => 'text/plain']];
echo $http_request->getResponseBody();
$ctx = stream_context_create($options);
file_put_contents($destination, $http_request->getResponseBody(),0,$ctx);
I've found that Google Cloud Storage supports uploading . I use $storage->objects->insert(...) which works fine,but I can't find a similar function to use for download.
Thank you!
Upvotes: 4
Views: 2077
Reputation: 6015
<?php
CloudStorageTools::serve('gs://mybucket/myfile.txt',
['save_as' => 'myfile.txt',
'content_type' => 'text/plain']););
See also the documentation and Stuart Langley's answer to this question.
Upvotes: 1