Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i list files / dirs in root directory with google drive api v3?

I am working on a website project. I have created an application on Google Developers Console and installed Google SDK on my server. Authorization done, offline access done, listing all files/dirs done, creating files done.

Now I would like to list files/dirs which is located in root directory. I mean only those not all child directories.

here is code sample:

    // $client is a Google_Client object
    $service = new Google_Service_Drive($client);
    // Print the names and IDs for up to 10 files.
    $optParams = array(
        'pageSize' => 10,
        'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
        'q' => "trashed=false"
    );
    $results = $service->files->listFiles($optParams);

    if (count($results->getFiles()) == 0) {
        print "No files found.\n";
    } else {

        print "Files:\n";
        foreach ($results->getFiles() as $file) {
            echo "Dosya Adı: ".$file->getName()."<br>";
            echo "Dosya Id: ".$file->getId()."<br>";
            echo "Tip: ".$file->getMimeType()."<br>";
            echo "Dosya Link: ".$file->getWebContentLink()."<br>";
            echo "Dosya Link: ".$file->getWebViewLink()."<br>";
        }
    }

this code listing all files/dirs (not in trash). i added "'root' in parents" to q but its give me nothing. didnt find anything useful in documentation.

here is the output:

Files:
Dosya Adı: 1.jpg
Dosya Id: **some-id**
Tip: image/jpeg
Dosya Link: https://docs.google.com/uc?id=**some-id**&export=download
Dosya Link: https://drive.google.com/file/d/**some-id**/view?usp=drivesdk


Dosya Adı: images2
Dosya Id: **some-id**
Tip: application/vnd.google-apps.folder
Dosya Link: 
Dosya Link: https://docs.google.com/folderview?id=**some-id**&usp=drivesdk


Dosya Adı: images
Dosya Id: **some-id**
Tip: application/vnd.google-apps.folder
Dosya Link: 
Dosya Link: https://docs.google.com/folderview?id=**some-id**&usp=drivesdk


Dosya Adı: sample_01.jpg
Dosya Id: **some-id**
Tip: image/jpeg
Dosya Link: https://docs.google.com/uc?id=**some-id**&export=download
Dosya Link: https://drive.google.com/file/d/**some-id**/view?usp=drivesdk


Dosya Adı: 1302.pdf
Dosya Id: **some-id**
Tip: application/pdf
Dosya Link: https://docs.google.com/uc?id=**some-id**&export=download
Dosya Link: https://drive.google.com/file/d/**some-id**/view?usp=drivesdk
like image 908
MC_delta_T Avatar asked Dec 19 '22 17:12

MC_delta_T


2 Answers

You can use "root" as an alias for your root directory ID:

$driveService->files->listFiles([
    // 'q' => '"'.MAIN_FOLDER_ID.'" in parents', // optionnal
    'q' => '"root" in parents', // optionnal
    'pageSize' => 100, // 1 to 1000
    'fields' => 'nextPageToken, files(id, name, mimeType, parents)'
]);
like image 159
Baptiste Clarey Sjöstrand Avatar answered Dec 21 '22 05:12

Baptiste Clarey Sjöstrand


I have found the solution finally

// $client is a Google_Client object
function rootFolderId($client) {
    $service = new Google_Service_Drive($client);

    $results = $service->files->get('root');
    return $results->getId();
}
like image 23
MC_delta_T Avatar answered Dec 21 '22 06:12

MC_delta_T