使用 google-api-php-client 下载文件 [英] Downloading files using google-api-php-client
问题描述
我在尝试使用 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);
响应类似于...
{
"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 但响应只包含元数据而没有对象/文件内容
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);
推荐答案
老问题,但它让我找到了正确的方向.selfLink
是元数据请求的链接,你需要 mediaLink
来获取实际的对象数据,它是 getAuth
而不是 getIo代码>.
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
.
此脚本将输出文件内容(假设您已经初始化了一个 $client
对象):
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();
这篇关于使用 google-api-php-client 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!