1
votes

I am having an issue with updating existing file in Google Drive using Google Drive API v3. From the code below create (uploading document first time) works just fine (else statement) however, update fails.

I have validated that fileId is correct.

return file.Id; throws an exception fill is null.

Any help is appreciated.

public string UploadFileToDrive(string fileName, string filePath, string contentType)
        {
            var fileMetadata = new File();
            fileMetadata.Name = fileName;
            fileMetadata.Parents = new List<string> { folderId };
            DriveService service = GetDriveService(credential);

            string query = "mimeType!='application/vnd.google-apps.folder' and trashed = false and name = '" + fileName + "'";
            FilesResource.ListRequest req;
            req = service.Files.List();
            req.Q = query;
            req.Fields = "files(id, name)";
            var result = req.Execute();

            if (result.Files.Count == 1)
            {
                FilesResource.UpdateMediaUpload updateRequest;
                string fileId = result.Files[0].Id;

                using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                    updateRequest = service.Files.Update(fileMetadata, fileId, stream, contentType);
                    updateRequest.Upload();
                    var file = updateRequest.ResponseBody;
                    return file.Id;
                };
            }
            else
            {
                FilesResource.CreateMediaUpload request;
                using (var stream = new FileStream(filePath, FileMode.Open))
                {
                    request = service.Files.Create(fileMetadata, stream, contentType);
                    request.Fields = "id";
                    request.Upload();
                    var file = request.ResponseBody;
                    return file.Id;
                };
            }
        }

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=EPGMonitor StackTrace: at EPGMonitor.GoogleDrive.UploadFileToDrive(String fileName, String filePath, String contentType) in C:\Users\vanja\Documents\Projects\EPG Monitor\EPGMonitor\EPGMonitor\GoogleDrive.cs:line 73 at EPGMonitor.Monitor.d__8.MoveNext() in C:\Users\vanja\Documents\Projects\EPG Monitor\EPGMonitor\EPGMonitor\Monitor.cs:line 51

1
please post the full exceptionDaImTo
here is the exception: System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=EPGMonitor StackTrace: at EPGMonitor.GoogleDrive.UploadFileToDrive(String fileName, String filePath, String contentType) in C:\Users\vanja\Documents\Projects\EPG Monitor\EPGMonitor\EPGMonitor\GoogleDrive.cs:line 73 at EPGMonitor.Monitor.<OnChanged>d__8.MoveNext() in C:\Users\vanja\Documents\Projects\EPG Monitor\EPGMonitor\EPGMonitor\Monitor.cs:line 51user2491457
what is result.FirstOrDefault()DaImTo

1 Answers

1
votes

I have resolved issue by initializing new File for Update vs. reusing File instance used by Create. Here is updated code:

if (result.Files.Count == 1)
{
    File updatedFileMetadata = new File();
    updatedFileMetadata.Name = fileName;

    FilesResource.UpdateMediaUpload updateRequest;
    string fileId = result.Files[0].Id;
    using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
    {
        updateRequest = service.Files.Update(updatedFileMetadata, fileId, stream, contentType);
        updateRequest.Upload();
        var file = updateRequest.ResponseBody;
        return file.Id;
    };
}