使用google-api-php-client下载文件

Car*_*ton 5 php cloud api json google-api-php-client

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

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

$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);
Run Code Online (Sandbox Code Playgroud)

回应就像......

{

 "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.........."
}
Run Code Online (Sandbox Code Playgroud)

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

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

bmc*_*vin 5

老问题,但它让我朝着正确的方向寻找。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();
Run Code Online (Sandbox Code Playgroud)


小智 -3

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