【问题标题】:Google Drive Service Account quota limitationGoogle 云端硬盘服务帐户配额限制
【发布时间】:2018-12-04 13:35:05
【问题描述】:

我正在将 C# WinForms(最终是 Windows 服务)中的大型磁盘映像文件上传到我的 Drive 中的一个文件夹中,并且目前正在使用 Google 服务帐户来执行此操作。

我的个人云端硬盘订阅了 2TB,但是在完成大文件(> 15GB,这是 ServiceAccount 配额)的上传后,它会引发异常,因为 ServiceAccount 的配额与我的个人帐户不同。

  1. 如何使用我的个人帐户而不是服务帐户进行身份验证,然后上传文件?这将取消配额限制。

  2. 我可以对正在上传的文件设置权限,以免出现此配额错误吗?

【问题讨论】:

    标签: c# google-drive-api google-authentication service-accounts quota


    【解决方案1】:

    解决方案是使用 OAuth,并通过浏览器对用户进行身份验证。

    UserCredential credential;
    
        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);
        }
    

    【讨论】:

      【解决方案2】:

      Dalmto 写了一篇关于如何使用 C# 上传到 Drive API 的教程:

      Google Drive API with C# .net – Upload

      if (System.IO.File.Exists(_uploadFile))
                  {
                      File body = new File();
                      body.Title = System.IO.Path.GetFileName(_uploadFile);
                      body.Description = "File uploaded by Diamto Drive Sample";
                      body.MimeType = GetMimeType(_uploadFile);
                      body.Parents = new List() { new ParentReference() { Id = _parent } };
      
                      // File's content.
                      byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
                      System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                      try
                      {
                          FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
                          request.Upload();
                          return request.ResponseBody;
                      }
                      catch (Exception e)
                      {
                          Console.WriteLine("An error occurred: " + e.Message);
                          return null;
                      }
                  }
                  else {
                      Console.WriteLine("File does not exist: " + _uploadFile);
                      return null;
                  }
      

      注意不建议将服务帐户用于处理大量数据,无论是下载/上传。

      【讨论】:

      • 虽然这对我的问题没有帮助:)
      • 如何使用我的个人帐户进行身份验证,然后上传文件?回答:使用您的个人帐户,这意味着记录您的个人电子邮件,是吗?
      • 是的,这就是我需要的,使用我的个人电子邮件。我在上面的代码或链接中看不到吗?可能错过了什么?
      • 使用.NET Quickstart中显示的OAuth。 OAuth2.0 将询问您放置个人电子邮件的凭据
      • OAuth 不弹出认证窗口吗?
      猜你喜欢
      • 2013-01-04
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多