【问题标题】:Is there a way to download a file from Google Cloud Storage in PHP ? Instead of just reading it有没有办法在 PHP 中从 Google Cloud Storage 下载文件?而不是仅仅阅读它
【发布时间】:2016-03-17 08:34:32
【问题描述】:

有没有办法用 PHP 从 Google Cloud Storage 中下载文件,而不是仅仅读取文件并使用 php 函数将内容保存到文件中?? p>

我的代码是这样的:

        $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 Cloud Storage 支持上传。我使用 $storage->objects->insert(...) 工作正常,但我找不到类似的函数用于下载。

谢谢!

【问题讨论】:

标签: php download google-cloud-storage


【解决方案1】:
<?php
CloudStorageTools::serve('gs://mybucket/myfile.txt',
                         ['save_as' => 'myfile.txt', 
                         'content_type' => 'text/plain']););

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

【讨论】:

  • 嗨。谢谢您的回复!但我认为这不能用于我的应用程序。我不知道原因,但我的 ApiClient 库没有 CloudStorageTools ...并且没有类似的东西。也许有这个 API 的不同版本?它似乎安装正确,因为我可以使用其他功能,如 ->insert() 进行上传
  • 我想我犯了一个错误...我正在寻找 AppEngine 的文档。但我不使用 AppEngine...似乎我使用的与 App Engine 不同。 cloud.google.com/storage/docs/json_api/v1/objects/get
  • 我猜你的问题把我吓跑了,因为从 GCS 读取文件并将其保存到本地文件正在下载它。唯一不起作用的地方是像 GAE 这样的环境,其中本地文件访问被禁用,您需要提供下载服务。如果您的意思不是在本地保存文件,也许您可​​以澄清一下“下载”的含义?
  • 您的意思是您希望您的应用提供从 GCS 读取的文件以供下载?
  • 我在寻求一种不同的方法。如果我阅读它并使用 file_put_contents 可能会有大文件的问题。我需要类似这样的服务功能,但对于我的 Google Storage APi 客户端。
猜你喜欢
  • 2022-01-23
  • 2018-12-17
  • 2016-08-19
  • 2019-08-06
  • 2017-11-08
  • 2023-03-29
  • 2014-01-28
  • 2020-11-10
  • 1970-01-01
相关资源
最近更新 更多