Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list of more than 1000 records from Google Drive API V3 in C#

This is the continuation of original question in this link.

Through the below code, I can able to fetch 1000 records but I have in total 6500++ records in my drive. Searching google but unable to find out the correct solution.

As per reference, the description value of Parameter "pageSize" is "The maximum number of files to return per page. Acceptable values are 1 to 1000, inclusive. (Default: 100)".

So it means, we can get only 1000 records or if possible, then what's the way. Also, I don't understand about the Parameter "pageToken", what's the usage of 'nextPageToken' value in realtime.

Code: (https://developers.google.com/drive/v3/web/quickstart/dotnet)

namespace gDrive
{
    class Program
    {
        static string[] Scopes = { DriveService.Scope.DriveReadonly };
        static string ApplicationName = "Drive API .NET Quickstart";

    static void Main(string[] args)
    {
        UserCredential credential;
        gDriveTableAdapter gDrive = new gDriveTableAdapter();

        using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            //Console.WriteLine("Credential file saved to: " + credPath);
        }

        // Create Drive API service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        FilesResource.ListRequest listRequest = service.Files.List();
        listRequest.PageSize = 1000;
        listRequest.Fields = "nextPageToken, files(webViewLink, name)";

        // List files.
        IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
            .Files;
        Console.WriteLine("Processing...\n");
        if (files != null && files.Count > 0)
        {
            foreach (var file in files)
            {
                gDrive.InsertQuery(file.Name, file.WebViewLink);
            }
            Console.WriteLine(files.Count + " records fetched.");
        }
        else
        {
            Console.WriteLine("No files found.");
        }
        Console.Read();
    }
  }
}
like image 474
Arun Avatar asked Jan 10 '17 15:01

Arun


People also ask

How do I get a list of files in Google Drive?

Goto the same Google Sheets File and refresh the page. Then you will get the "List Files/Folders" menu, click on it, and select the "List All Files and Folders" item.

How do I see the number of files in Google Drive?

Right-click on a folder and select 'Download'. You'll often (but not always) see a count of the included files. Open a folder, scroll to the end of the list, type A to select all, choose 'More' and select 'Move to'. That will give you a total number of files, after which you can cancel the move.

Is Pydrive deprecated?

Deprecated. This project is deprecated and no longer maintained. No further changes will be made.


Video Answer


2 Answers

If you need to download paginating you can achieve it via C# SDK too. The trick is in keeping track of the listRequest.Execute() return value, that contains several variables and between them the NextPageToken. This part is "hidden" in the standard google example.

FilesResource.ListRequest listRequest = _service.Files.List();
listRequest.PageSize = 100;
listRequest.Fields = "nextPageToken, files(id, name)";

// List files.
var result =  listRequest.Execute();
IList<Google.Apis.Drive.v3.Data.File> files =result.Files;
Console.WriteLine("Files:");
while (files!=null && files.Count > 0)
{
    foreach (var file in files)
    {
        Console.WriteLine("{0} ({1})", file.Name, file.Id);
    }
    if (!string.IsNullOrWhiteSpace(result.NextPageToken))
    {
        listRequest = _service.Files.List();
        listRequest.PageToken = result.NextPageToken;
        listRequest.PageSize = 100;
        listRequest.Fields = "nextPageToken, files(id, name)";
        result = listRequest.Execute();
        files = result.Files;
    }
}
like image 51
Kendar Avatar answered Oct 31 '22 06:10

Kendar


Here is an improved (IMO) version of EDR's great answer that does not repeat the listRequest code:

List<Google.Apis.Drive.v3.Data.File> allFiles = new List<Google.Apis.Drive.v3.Data.File>();

Google.Apis.Drive.v3.Data.FileList result = null;
while (true)
{
    if (result != null && string.IsNullOrWhiteSpace(result.NextPageToken))
        break;

    FilesResource.ListRequest listRequest = service.Files.List();
    listRequest.PageSize = 1000;
    listRequest.Fields = "nextPageToken, files(id, name)";
    if (result != null)
        listRequest.PageToken = result.NextPageToken;

    result = listRequest.Execute();
    allFiles.AddRange(result.Files);
}
like image 25
Andriod Avatar answered Oct 31 '22 06:10

Andriod