【问题标题】:Google Drive API extremely slowGoogle Drive API 非常慢
【发布时间】:2021-04-02 01:14:57
【问题描述】:

我使用从 Google 自己的 .NET quickstart 复制而来的以下代码来列出您的 Google Drive 帐户中的文件(身份验证部分已被调整为使用通过遵循here 概述的步骤获得的刷新令牌) .

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using System;
using System.Diagnostics;

namespace GoogleTest {
    class Program {
        [STAThread]
        static void Main(string[] args) {
            var sw = Stopwatch.StartNew();
            var secrets = new ClientSecrets() {
                ClientId = "paste-your-client-id-here",
                ClientSecret = "paste-your-client-secret-here"
            };
            var token = new TokenResponse { RefreshToken = "paste-your-refresh-token-here" };
            var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
                new GoogleAuthorizationCodeFlow.Initializer {
                    ClientSecrets = secrets
                }),
                "user",
                token);

            Console.WriteLine($"1: {sw.Elapsed}");
            // Create the service.
            var service = new DriveService(new BaseClientService.Initializer() {
                HttpClientInitializer = credentials
            });
            Console.WriteLine($"2: {sw.Elapsed}");
            // Define parameters of request.
            var listRequest = service.Files.List();
            listRequest.PageSize = 10;
            listRequest.Fields = "nextPageToken, files(id, name)";

            Console.WriteLine($"3: {sw.Elapsed}");
            // List files.
            var files = listRequest.Execute().Files;
            Console.WriteLine($"4: {sw.Elapsed}");
            if (files != null && files.Count > 0) {
                foreach (var file in files) {
                    Console.WriteLine("{0} ({1})", file.Name, file.Id);
                }
            } else {
                Console.WriteLine("No files found.");
            }
            Console.WriteLine($"5: {sw.Elapsed}");
        }
    }
}

运行上述代码会产生以下几行的输出:

1: 00:00:00.1075210
2: 00:00:00.1881154
3: 00:00:00.1987953
4: 00:00:12.5158776
No files found.
5: 00:00:12.5166778
Press any key to continue . . .

换句话说,var files = listRequest.Execute().Files 调用需要 10 秒或更长时间。通常它甚至需要长达 20 秒。

对于这样一个微不足道的请求,这肯定不可能是正常的响应时间。我在这里错过了什么吗?如您所见,相关的 Google Drive 帐户甚至不包含任何文件并且完全为空。

【问题讨论】:

  • 检查您的互联网连接,尝试 ping 服务器并查看您获得的响应时间?听起来像是网络问题而不是编程问题。但请记住,Google Workspace API 并不是世界上最快的(尽管我同意 10-20 秒慢) - 我的意思是,它们可以免费使用,对吗?
  • 据我所知,我的互联网连接很好。我在两台不同的机器上运行了测试……一台托管多个网站的服务器和我家的电脑。没什么大不了的,我猜我只是期望从谷歌服务中获得更多。
  • 你有多少个文件?您可能只能检索 10 个,但云端硬盘会扫描您的所有文件,然后再决定返回哪 10 个。

标签: performance google-api google-drive-api google-oauth google-api-dotnet-client


【解决方案1】:

Google API 的响应时间取决于很多变量。

  1. 在您来电的同时,其他用户正在服务器上做什么
  2. 您在一天中的什么时间打来电话会增加其他人也在运行他们的 cron 作业的机会。
  3. 您的网速。

您需要记住,这是一个免费的 api,您得到您所支付的。是的,有时电话回复很慢,但您无需为此付费。

【讨论】:

  • 是的,我想你是对的,尽管我认为所有公司中的谷歌肯定会在技术和财务上处于保证更好响应时间的位置,无论一天中的时间或用户数量如何.其他所有公司都能做到。我也是付费客户,所以在我的情况下它不是免费的。
  • @user3700562 对不要对免费 API 抱太大期望的警告的正确答复是,Google 应该提供一个有效的付费 API——或者修复该死的东西。如果他们不提供有效的付费服务,则无需高谈阔论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 2020-04-05
  • 2016-06-15
  • 1970-01-01
  • 2015-12-19
相关资源
最近更新 更多