David M
David M

Reputation: 31

Google Drive watching for new files

I'm considering using Google Drive push notification in order to replace our currently pulling process. I started playing with it, but I have 2 major problems:

Watching changes:

When watching for drive changes, I get notification with the new change id. But when I try to query it using: driveService.changes().get(changeId), I intermittently get 404. Am I doing something wrong here?

Watching files:

When watching for file changes, in case of a folder, I want to know about new files added to that folder, so I expected that when adding/removing files from this folder, the "x-goog-resource-state" will hold "add/remove" value while "x-goog-changed" will contain "children".

In reality, the "x-goog-changed" does contain "children", but the "x-goog-resource-state" is always "update", and there is no extra information about the added/deleted file.

Regarding deleted files, I know can get it by watching the file once I have it, but is there a way I can get updated about new files in a certain folder?

Upvotes: 3

Views: 3693

Answers (1)

Meriam-K
Meriam-K

Reputation: 126

I was working on a similar project a few months ago. There are two things you can do to monitor changes on Google Drive :

  1. Set Notification Push using : changes().watch()
  2. Set Notification Push using : files().watch()

The 1st case sends you a request for everything that happens on the Drive you are monitoring, with very little information on what exactly has changed.

The 2nd case is less 'spamming', and you get to decide which folder to monitor.

However the tags on the change type are not accurate. when I was using files().watch() I tested all the use-cases, and I compared the headers of each case.

My conclusions are:

for a new file (or folder) creation inside yourfolder (yourfolder/newfile) the headers contain: 'X-Goog-Changed': 'properties' 'X-Goog-Resource-State': 'update'

which is the same when you move a file to yourfolder, or when you start following an existing file in your folder.

you get 'X-Goog-Resource-State': 'add' when you share with a user

as you can see, the header tags are not accurate/unique.

Also, note that the push-notification channel will not send you requests for files inside a folder inside yourfolder (yourfolder/folder/files). And the channel will expire at some point.

If you still have any questions, or want to know how to implement the code, let me know : )

Upvotes: 5

Related Questions