使用谷歌的API的PHP客户端下载文件 [英] Downloading files using google-api-php-client

查看:199
本文介绍了使用谷歌的API的PHP客户端下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,尝试使用在的 HTTPS://$c$c.google.com/p/google-api-php-client/

我已经验证自己确定,使用下面的code我可以返回包含链接到我的文件对象

  $这个 - > storageService =新Google_StorageService($这个 - >客户端);
$这个 - >对象= $这个 - > storageService->对象;$选项=数组(
    'preFIX'=> REPORT_NAME_2013-07-01
);
$ bucket_contents = $这个 - >对象 - > listObjects($斗,$选项);

响应是一样的东西...

  { 种:存储#对象
 ID:<斗> /<报告与GT;的.csv / 1001,
 selfLink:https://www.googleapis.com/storage/v1beta2/b/<bucket>/o/<report>.csv,
 名:&LT;报告与GT; .csv文件,
 斗:&LT;斗&gt;中,
 代:1001,
 metageneration:1,
 的contentType:应用程序/ CSV
 更新:2013-07-22T10:21:08.811Z
 大小:806
 md5Hash:wT01i ......,
 的MediaLink: \"https://www.googleapis.com/storage/v1beta2/b/<bucket>/o/<report>.csv?generation=1001&alt=media\",
 所有者:{
  实体:用户-00B ........
  ENTITYID:00b490 ......
 },
 CRC32C:8Y ........,
 ETAG:CPjZ ..........
}

但我怎么去下载使用谷歌PHP客户端的文件......我不能使用的file_get_contents因为它没​​有的验证细节知识。
我发现最好的事情是什么,使用Google_Client但响应只包含元数据,并没有对象/文件内容

  $ =请求新Google_Htt prequest($对象['selfLink']);
$响应= $这个 - &GT;客户 - &GT; GETIO() - GT; authenticatedRequest($请求);


解决方案

老问题,但它让我找对了方向。 selfLink 是链接到元数据请求,您需要的MediaLink 来获得实际的对象数据,它的 getAuth ,而不是 GETIO

该脚本将输出文件的内容(因为你已经初始化一个 $客户端)对象:

  $ =服务新Google_Service_Storage($客户端);
$对象= $服务 - &GT;对象 - &GT;获取('bucketname','对象名');
$ =请求新Google_Http_Request($对象 - &GT; getMediaLink());
$响应= $客户 - &GT; getAuth() - GT; authenticatedRequest($请求);
回声$响应 - &GT; getResponseBody();

I'm having an issue trying to download a file from Google Cloud Storage using the php client found at https://code.google.com/p/google-api-php-client/

I have authenticated myself ok and using the following code I can return an object which contains the link to my file

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

The response is something like...

{

 "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.........."
}

But how do I go about downloading the file using the Google PHP client...I can't use a file_get_contents as it has no knowledge of the authentication details. The best thing I have found is something that uses the Google_Client but the response simply contains meta data and no object/file content

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

解决方案

Old question, but it got me looking in the right direction. selfLink is the link to the metadata request, you need mediaLink to get the actual object data, and it's getAuth rather than getIo.

This script will output the file contents (given you have already initialised a $client object) :

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

这篇关于使用谷歌的API的PHP客户端下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆