0
votes

I'm uploading large disk image files in C# WinForms (eventually Windows Service) into a folder in my Drive, and am currently using a Google Service account to do so.

I have a 2TB subscription for my personal Drive, however upon completion of upload of large files ( > 15GB which is the ServiceAccount quota) it throws an exception because the ServiceAccount does not have the same quota as my personal account.

  1. How can I authenticate, and then upload files using my personal account, instead of the service account? This will remove the quota restriction.

    OR

  2. Can I set permissions on the file I'm uploading so that I don't get this quota error?

2

2 Answers

0
votes

Dalmto wrote a tutorial on how to upload to Drive API using C#:

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

Note: Service Accounts are not advisable to be used in processing large amounts of data, be it downloading/uploading.

0
votes

The solution was to use OAuth, and authenticate the user via browser.

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