[英]Saving images from Google Drive via API
我遵循了本教程 ,一切正常,我可以看到名稱和ID的列表。
當我嘗試將此圖像保存在服務器上時,保存的文件為空。 我從命令行運行此腳本。
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 1,
'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($results->getFiles() as $file) {
printf("%s (%s)\n", $file->getName(), $file->getId());
$fileIdr = $file->getId();
}
// $content = $service->files->export($fileIdr, 'image/jpeg', array('alt' => 'media' ));
$content = $service->files->get($fileIdr, array('alt' => 'media' ));
var_dump($content);
$zdjecie = '/home/bachus03/domains/bachus03.vot.pl/public_html/fb/public/test.jpg';
file_put_contents($zdjecie, $content);
要獲取文件內容,請使用帶有getDownloadUrl()函數的Google示例。 來源在這里
/*
*
* Print a file's metadata.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param string $fileId ID of the file to print metadata for.
*/
function printFile($service, $fileId) {
try {
$file = $service->files->get($fileId);
print "Title: " . $file->getTitle();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType(); } catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
/*
*
* Download a file's content.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param File $file Drive File instance.
* @return String The file's content if successful, null otherwise.
*/
function downloadFile($service, $file) {
$downloadUrl = $file->getDownloadUrl();
if ($downloadUrl) {
$request = new Google_Http_Request($downloadUrl, 'GET', null, null);
$httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
return $httpRequest->getResponseBody();
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
編輯:查看文件資源參考: https : //developers.google.com/drive/v3/reference/files#resource您可以使用webContentLink屬性獲取我認為的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.