【问题标题】:Downloading files using google-api-php-client使用 google-api-php-client 下载文件
【发布时间】:2013-07-25 05:01:43
【问题描述】:

我在尝试使用位于 https://code.google.com/p/google-api-php-client/ 的 php 客户端从 Google Cloud Storage 下载文件时遇到问题

我已经验证了自己,并且使用以下代码我可以返回一个包含指向我的文件的链接的对象

$this->storageService = new Google_StorageService($this->client);
$this->objects = $this->storageService->objects;

$options = array(
    'prefix' => 'REPORT_NAME_2013-07-01'
);
$bucket_contents = $this->objects->listObjects($bucket, $options);

响应类似于...

{

 "kind": "storage#object",
 "id": "<bucket>/<report>.csv/1001",
 "selfLink": "https://www.googleapis.com/storage/v1beta2/b/<bucket>/o/<report>.csv",
 "name": "<report>.csv",
 "bucket": "<bucket>",
 "generation": "1001",
 "metageneration": "1",
 "contentType": "application/csv",
 "updated": "2013-07-22T10:21:08.811Z",
 "size": "806",
 "md5Hash": "wT01i....",
 "mediaLink": "https://www.googleapis.com/storage/v1beta2/b/<bucket>/o/<report>.csv?generation=1001&alt=media",
 "owner": {
  "entity": "user-00b........",
  "entityId": "00b490......."
 },
 "crc32c": "8y........",
 "etag": "CPjZ.........."
}

但是如何使用 Google PHP 客户端下载文件...我不能使用 file_get_contents,因为它不知道身份验证详细信息。 我发现的最好的东西是使用 Google_Client 但响应只包含元数据而没有对象/文件内容

$request = new Google_HttpRequest($object['selfLink']);
$response = $this->client->getIo()->authenticatedRequest($request);

【问题讨论】:

    标签: php json api cloud google-api-php-client


    【解决方案1】:

    老问题,但它让我找到了正确的方向。 selfLink是元数据请求的链接,你需要mediaLink来获取实际的对象数据,它是getAuth而不是getIo

    这个脚本会输出文件内容(假设你已经初始化了一个$client对象):

    $service = new Google_Service_Storage($client);
    $object = $service->objects->get('bucketname', 'objectname');
    $request = new Google_Http_Request($object->getMediaLink());
    $response = $client->getAuth()->authenticatedRequest($request);
    echo $response->getResponseBody();
    

    【讨论】:

      【解决方案2】:

      这对 apiclient ~2.0 无效,请参阅 github 中的 UPGRADING.md 文件。

      使用 apiclient ~2.0 的工作代码应该是:

      $service = new Google_Service_Storage($client);
      $object = $service->objects->get('bucketname', 'objectname');
      
      // create an authorized HTTP client
      $httpClient = $client->authorize();
      
      $response = $httpClient->request('GET', $object->getMediaLink());
      
      echo $response->getBody();
      

      或授权现有的 Guzzle 客户端:

      $service = new Google_Service_Storage($client);
      $object = $service->objects->get('bucketname', 'objectname');
      
      // add authorization to an existing client
      $httpClient = new GuzzleHttp\Client();
      $httpClient = $client->authorize($httpClient);
      
      $response = $httpClient->request('GET', $object->getMediaLink());
      
      echo $response->getBody();
      

      【讨论】:

        【解决方案3】:

        要下载文件,您需要授予 allUsers 的 READER 访问权限(您可以从 google web 控制台执行此操作或使用 google php api)

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-12
        • 2015-02-06
        相关资源
        最近更新 更多