Google Drive API v3 - 下载 PHP 中的文件
Google Drive API v3 - downloading files in PHP
我正在尝试使用 PHP 了解 Google Drive API v3 的下载流程。使用 API v2 下载文件 I:
- 获取文件元数据
- 使用 downloadUrl 参数获取文件的直接 link,附加一个 oAuth 令牌并向其发出 GET 请求。
使用 API v3 这似乎已被弃用,并且根据 docs 您在驱动器服务上调用 files->get()
并使用数组参数 "alt" => "media"
来获取文件本身而不是元数据。
他们的例子是:
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
'alt' => 'media' ));
虽然我无法理解它是如何工作的,但我已经浏览了代码,但它没有提供更多信息。
当您调用 get()
时,示例中实际进入 $content
的是什么?它是文件的内容(在这种情况下,处理大文件时这似乎很麻烦 - 你肯定会内存不足?!)还是我可以调用 fopen
的某种类型的流引用?我如何将这个文件保存到磁盘?
文档并没有真正详细说明当您进行 API 调用时会发生什么,它只是说它执行文件下载?
经过一些试验我明白了。
当您使用文档中指定的 alt=>media
参数调用 get()
方法时,您将获得基础 HTTP 响应,即 Guzzle response object(显然客户端库使用 Guzzle因为它是底层传输)。
从那里您可以调用任何 Guzzle 响应方法,例如 $response->getStatusCode()
或者您可以获得实际文件内容的流。
如果他们在某处记录了这一点会很有帮助!
编辑:这是一个粗略的例子,如果其他人对如何保存文件感到困惑。
<?php
date_default_timezone_set("Europe/London");
require_once 'vendor/autoload.php';
// I'm using a service account, use whatever Google auth flow for your type of account.
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Drive($client);
$fileId = "0Bxxxxxxxxxxxxxxxxxxxx"; // Google File ID
$content = $service->files->get($fileId, array("alt" => "media"));
// Open file handle for output.
$outHandle = fopen("/path/to/destination", "w+");
// Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.
while (!$content->getBody()->eof()) {
fwrite($outHandle, $content->getBody()->read(1024));
}
// Close output file handle.
fclose($outHandle);
echo "Done.\n"
?>
我正在尝试使用 PHP 了解 Google Drive API v3 的下载流程。使用 API v2 下载文件 I:
- 获取文件元数据
- 使用 downloadUrl 参数获取文件的直接 link,附加一个 oAuth 令牌并向其发出 GET 请求。
使用 API v3 这似乎已被弃用,并且根据 docs 您在驱动器服务上调用 files->get()
并使用数组参数 "alt" => "media"
来获取文件本身而不是元数据。
他们的例子是:
$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
'alt' => 'media' ));
虽然我无法理解它是如何工作的,但我已经浏览了代码,但它没有提供更多信息。
当您调用 get()
时,示例中实际进入 $content
的是什么?它是文件的内容(在这种情况下,处理大文件时这似乎很麻烦 - 你肯定会内存不足?!)还是我可以调用 fopen
的某种类型的流引用?我如何将这个文件保存到磁盘?
文档并没有真正详细说明当您进行 API 调用时会发生什么,它只是说它执行文件下载?
经过一些试验我明白了。
当您使用文档中指定的 alt=>media
参数调用 get()
方法时,您将获得基础 HTTP 响应,即 Guzzle response object(显然客户端库使用 Guzzle因为它是底层传输)。
从那里您可以调用任何 Guzzle 响应方法,例如 $response->getStatusCode()
或者您可以获得实际文件内容的流。
如果他们在某处记录了这一点会很有帮助!
编辑:这是一个粗略的例子,如果其他人对如何保存文件感到困惑。
<?php
date_default_timezone_set("Europe/London");
require_once 'vendor/autoload.php';
// I'm using a service account, use whatever Google auth flow for your type of account.
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Drive($client);
$fileId = "0Bxxxxxxxxxxxxxxxxxxxx"; // Google File ID
$content = $service->files->get($fileId, array("alt" => "media"));
// Open file handle for output.
$outHandle = fopen("/path/to/destination", "w+");
// Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.
while (!$content->getBody()->eof()) {
fwrite($outHandle, $content->getBody()->read(1024));
}
// Close output file handle.
fclose($outHandle);
echo "Done.\n"
?>