Home > Software engineering >  check if file is opened Google Drive API
check if file is opened Google Drive API

Time:03-26

I need to write inside a file located in a folder of my account in google drive and I wrote a desktop application in VB.net using Google Driver API v3. Since that me and my friend use the same account in different PC with the same VB.net application that I wrote, I need your help to to check if that file is opened by another user to avoid opening and garantee an "exclusive opening" on it.

I dont' know if google drive as an option to set an "exclusive opening" instead of API.

Maybe I can set permission to "read only" on the file when is opened by my application but I think it is not a good thing because if my application crashes (for example for a blackout) my file remain in read only state for always.

Can you help me ?

That's my routine that make a connection on my Google drive with my credentials, search for my folder and my file and give me back their ID:

Public Sub GoogleDrive()  
Dim credential As UserCredential  
Dim ID_Folder As String = ""  
Dim ID_File As String = ""

Using Stream = New FileStream("credenzials.json", FileMode.Open, FileAccess.Read)  
'The file token.json stores the user's access and refresh tokens, and is created  
'automatically when the authorization flow completes for the first time.  
Dim credPath As String = "token.json"  
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(  
GoogleClientSecrets.Load(Stream).Secrets,  
Scopes,  
"user",  
CancellationToken.None,  
New FileDataStore(credPath, True)).Result  
Console.WriteLine("Credential file saved to: "   credPath)

End Using  
'Create Drive API service.  
Dim Service = New DriveService(New BaseClientService.Initializer() With  
{  
.HttpClientInitializer = credential,  
.ApplicationName = ApplicationName  
})  
' Define parameters of request.  
Try  
Dim findrequest As FilesResource.ListRequest = Service.Files.List()  
findrequest.PageSize = 10  
findrequest.Fields = "nextPageToken, files(id, name)"  
findrequest.Spaces = "drive"  
Dim listFolder As Data.FileList = findrequest.Execute()

If listFolder.Files.Count > 0 Then  
For Each item In listFolder.Files  
If item.MimeType = "application/vnd.google-apps.folder" Then  
If item.Name = "MyFolder" Then  
ID_Folder = item.Id.ToString  
End If  
End If  
'  
If item.MimeType = "application/msaccess" Then  
If item.Name = "myFile.mdb" Then  
ID_File = item.Id.ToString  
End If  
End If

If (ID_File <> "") And (ID_Folder <> "") Then  
'How check if ID_File is opened by an other user ?  
Exit For  
End If  
Next  
End If

Catch ex As Exception  
'MsgBox(ex.Message)  
Throw ex

End Try  
End Sub

CodePudding user response:

The google drive api is a file storage api it gives you the ability to List, create, delete, Upload and download files.

It does not give you the ability to edit the contents of those files.

It cant tell you if anyone has a file open or not.

CodePudding user response:

Like mentioned before, there is no way to check whether a file is open or not by using the Drive API.

However, you can file a feature request on Google's Issue Tracker here if you would like to have this available.

  • Related