有没有办法从PHP下载Google云端存储文件?而不只是阅读它

时间:2016-03-17 08:34:33

标签: php download google-cloud-storage

有没有办法在PHP中下载来自Google云端存储的文件,而不仅仅是阅读它并使用php函数将内容保存到文件

我的代码是:

        $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);

我发现Google云端存储支持上传。我使用 $ storage-> objects-> insert(...),它工作正常,但我找不到类似的功能可供下载。

谢谢!

1 个答案:

答案 0 :(得分:1)

<?php
CloudStorageTools::serve('gs://mybucket/myfile.txt',
                         ['save_as' => 'myfile.txt', 
                         'content_type' => 'text/plain']););

另请参阅此问题的documentation和Stuart Langley answer