【问题标题】:How to get Google drive file by folder name or folder ID?如何通过文件夹名称或文件夹 ID 获取 Google 驱动器文件?
【发布时间】:2019-02-11 10:21:36
【问题描述】:

我正在使用 Google API 客户端库。

我不明白如何根据文件夹名称或文件夹 ID 获取文件列表,例如:-

驱动器文件夹结构

Main =>
    - Demo 1
        - img.png
        - img2.png
    - Demo 2
        - img_Demo 2.png
        - img2_Demo 2.png
    - Demo 3
        - img_Demo 3.png
        - img2_Demo 3.png

现在我使用以下代码获取主文件夹列表和 ID

<?php
// include your composer dependencies
require_once 'vendor/autoload.php';
#https://developers.google.com/api-client-library/php/auth/web-app

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

$client->setAccessToken('AccessToken');

$drive = new Google_Service_Drive($client);

$files_list = $drive->files->listFiles(array())->getFiles($optParams);

echo "<pre>";
print_r($files_list);
exit;
?>

我得到了这个输出:

Array
(
    [0] => Google_Service_Drive_DriveFile Object
        (
            [id] => 1LKMiCuI3OlUYrFlQnl
            [imageMediaMetadataType:protected] => Google_Service_Drive_DriveFileImageMediaMetadata
            [imageMediaMetadataDataType:protected] => 
            [isAppAuthorized] => 
            [kind] => drive#file
            [lastModifyingUserType:protected] => Google_Service_Drive_User
            [mimeType] => application/vnd.google-apps.folder
            [name] => main
            )
        )

【问题讨论】:

    标签: php google-api google-drive-api google-api-php-client


    【解决方案1】:

    File.list 有 q 参数,它允许你 search 文件。以下将返回父 ID 为 1234567 的所有文件,该文件是您要查找的目录的文件 ID。

    '1234567' in parents
    

    您可以将 q 参数作为 optParams 的一部分发送

    $optParams = array( 'q' => "'1234567' in parents" ); 
    $files_list = $drive->files->listFiles($optParams); 
    

    【讨论】:

    • 我该怎么做?这有什么作用吗?
    • $optParams = array('q' => "1LKMiCuI3qNCMsdsfdfsW0EspsNFCOlUYrFlQnl"); $files_list = $drive->files->listFiles($optParams);
    • 检查我的编辑应该更像是尝试在此页面上进行测试您可以使用左侧的 q 参数developers.google.com/drive/api/v3/reference/files/list
    • 你拯救了我的一天。谢谢
    • @DaImTo,如何一次使用多个文件夹 ID 获取文件。假设我有 20 个文件夹 ID,我只想从这些文件夹中获取所有文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2022-12-18
    • 1970-01-01
    • 2013-08-25
    相关资源
    最近更新 更多