如何在C#中列出来自Google Drive API V3的1000多条记录 [英] How to list of more than 1000 records from Google Drive API V3 in C#

查看:101
本文介绍了如何在C#中列出来自Google Drive API V3的1000多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 link

通过下面的代码,我可以获取1000条记录,但我有总共有6500 ++记录在我的驱动器中。搜索谷歌,但无法找到正确的解决方案。根据参考,参数pageSize的描述值是每页返回的最大文件数量,可接受的值是1到1000,包括(默认值)。 :100)。

所以这意味着,我们只能获得1000条记录,或者如果可能的话,那么这是怎么回事。此外,我不明白参数pageToken,实时使用'nextPageToken'的值是什么。

代码:( https://developers.google.com/drive/v3/web/quickstart/dotnet

 命名空间gDrive 
{
class程序
{
静态字符串[]作用域= {DriveService.Scope.DriveReadonly};
static string ApplicationName =Drive API .NET Quickstart;

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

使用(var stream =
new FileStream(client_secret.json,FileMode.Open,FileAccess.Read))
{
字符串credPath = System.Environment .GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath,.credentials / drive-dotnet-quickstart.json);

凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(流).Secrets,
作用域,
用户,
CancellationToken.None,
新的FileDataStore(credPath,true))。
//Console.WriteLine(\"Credential file saved to:+ credPath);
}

//创建Drive API服务。
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName = ApplicationName,
});

//定义请求的参数。
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 1000;
listRequest.Fields =nextPageToken,files(webViewLink,name);

//列出文件。
IList< Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
Console.WriteLine(Processing ... \\\
);
if(files!= null&& file.Count> 0)
{
foreach(文件中的var文件)
{
gDrive.InsertQuery( file.Name,file.WebViewLink);
}
Console.WriteLine(files.Count +记录提取。);
}
else
{
Console.WriteLine(No files found。);
}
Console.Read();




解决方案

通过Google脚本实现。感谢mesgarpour(链接)。

  var folderId =在此输入文件夹ID; 

//主要功能1:列出所有文件夹,&写入当前表单。
函数listFolders(){
getFolderTree(folderId,false);
};

//主要功能2:列出所有文件&文件夹,&写入当前表单。
函数listAll(){
getFolderTree(folderId,true);
};

// =================
//获取文件夹树
函数getFolderTree(folderId,listAll){
尝试{
//如果您想从顶部(根)文件夹搜索
var parentFolder = DriveApp.getRootFolder();

//如果您想要任何子文件夹的树
// var parentFolder = DriveApp.getFolderById(folderId);

//初始化工作表
var file,data,sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow([Full Path,Name,Date,URL,Last Updated,Description,Size]);

//获取文件和文件夹
getChildFolders(parentFolder.getName(),parentFolder,data,sheet,listAll);

} catch(e){
Logger.log(e.toString());
}
};

//以递归模式获取文件和文件夹列表及其元数据
函数getChildFolders(parentName,parent,data,sheet,listAll){
var childFolders = parent。 getFolders();
$ b $ //列出文件夹中的文件夹
while(childFolders.hasNext()){
var childFolder = childFolders.next();
// Logger.log(文件夹名称:+ childFolder.getName());
data = [
parentName +/+ childFolder.getName(),
childFolder.getName(),
childFolder.getDateCreated(),
childFolder.getUrl (),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()
];
//写入
sheet.appendRow(data);

//列出文件夹中的文件
var files = childFolder.getFiles();
while(listAll& files.hasNext()){
var childFile = files.next();
// Logger.log(File Name:+ childFile.getName());
data = [
parentName +/+ childFolder.getName()+/+ childFile.getName(),
childFile.getName(),
childFile.getDateCreated (),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()
];
//写入
sheet.appendRow(data);

$ b $ //递归调用子文件夹
getChildFolders(parentName +/+ childFolder.getName(),childFolder,data,sheet,listAll);
}
};


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();
    }
  }
}

解决方案

Achieved thru Google Script. Thanks to mesgarpour (link).

var folderId = "Enter the Folder Id here";

// Main function 1: List all folders, & write into the current sheet.
function listFolders(){
  getFolderTree(folderId, false);
};

// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
  getFolderTree(folderId, true); 
};

// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
  try {
    // If you want to search from the top (root) folder
    var parentFolder = DriveApp.getRootFolder();

    // If you want a tree of any sub folder
    //var parentFolder = DriveApp.getFolderById(folderId);

    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]);

    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);

  } catch (e) {
    Logger.log(e.toString());
  }
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();

  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    // Logger.log("Folder Name: " + childFolder.getName());
    data = [ 
      parentName + "/" + childFolder.getName(),
      childFolder.getName(),
      childFolder.getDateCreated(),
      childFolder.getUrl(),
      childFolder.getLastUpdated(),
      childFolder.getDescription(),
      childFolder.getSize()
    ];
    // Write
    sheet.appendRow(data);

    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      // Logger.log("File Name: " + childFile.getName());
      data = [ 
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        childFile.getDescription(),
        childFile.getSize()
      ];
      // Write
      sheet.appendRow(data);
    }

    // Recursive call of the subfolder
    getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
  }
};

这篇关于如何在C#中列出来自Google Drive API V3的1000多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆