4

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!

AdiCrainic
  • 276
  • 1
  • 7
  • 18
  • Possible duplicate of [Serving files from Google Cloud Store on a GAE PHP site](http://stackoverflow.com/questions/22128709/serving-files-from-google-cloud-store-on-a-gae-php-site) – Adam Mar 20 '16 at 20:58

1 Answers1

1
<?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.

Community
  • 1
  • 1
Adam
  • 5,697
  • 1
  • 20
  • 52
  • Hi.Thank you for your response ! But I think this cannot be used for my app.I don't know the reason but my ApiClient library doesn't have CloudStorageTools ...and nothing similar to it. Maybe a have a different version of this API? It seems to be installed correctly because i can use another functions like ->insert() for uploading – AdiCrainic Mar 21 '16 at 10:49
  • I think I made a mistake...I was looking for documentation for AppEngine.But I don't use AppEngine...Seem that what i use is different that App Engine. https://cloud.google.com/storage/docs/json_api/v1/objects/get – AdiCrainic Mar 21 '16 at 11:26
  • I guess your question threw me off then, because reading a file from GCS and saving it to a local file IS downloading it. The only place this wouldn't work would be an environment like GAE where local file access is disabled, and you need to serve the download. Perhaps you could clarify what you mean by 'download' if you don't mean saving the file locally? – Adam Mar 22 '16 at 22:39
  • Do you mean that you want your app to serve the file it reads from GCS for downloading? – Adam Mar 22 '16 at 22:45
  • I was asking for a different approach.If I read it and use file_put_contents maybe there will be a problem with large files. I needed something like this serve function,but for my Google Storage APi Client. – AdiCrainic Mar 23 '16 at 09:20
  • Ah, this is something that comes up in PHP sometimes, as HttpRequest::getResponseBody() returns a string instead of a stream resource. This means it needs to read the entire response into memory first. PHP doesn't provide something like a HttpRequest::getResponseBodyAsStream() method that would solve this problem. There is a somewhat hairy solution to create a custom stream wrapper, as mentioned here: http://stackoverflow.com/a/1342760/3953357 – Adam Mar 25 '16 at 18:23