How do I restore deleted documents from shared Google Drive folders? - google-drive-api

A non-privileged Google Drive user has accidentally removed a large number of files from folders shared across an organisation. They do not have permission to delete the files entirely, because they are not the owner. However, users with edit permissions are able to remove a file from a shared folder. This returns the user to the owner, but seems to leave the file orphaned without a parent folder.
The files were owned by various different users.
How do I restore these files to their correct folders? The Google Drive Audit Log does not contain enough information to restore the folders correctly - the parent folder ID is not included with the "Remove from folder" event.

Google Drive is included in the Reports API of the Google Apps Admin SDK. It provides similar information to the Google Drive Audit Log, but with additional metadata. That includes the parent folder ID of files which were removed.
To restore the files you should first query the Reports API for files removed by the user in question over the relevant time period, using the Activities:list method.
Then you'll need to setup a Google Apps service account (which is a little confusing), to allow you to impersonate the owners of the documents that were removed.
Lastly, you can iterate over the event report for the removed files and use the Files: patch method in Google Drive REST API, to re-add the parent ID's to each of the files.
See Gist Using Google Drive API to restore files removed from shared folders
for example of the last step.

Related

How to know through APIs who created the file in Shared Drive (Team Drive) in Google Drive

All the files in Shared drive (Team Drive) in google drive are accessible by all users who have access to Shared Drive (Team Drive).
When a new file is created in a Shared Drive(Team Drive) by default all the users with access to Shared Drive(Team Drive) will become owners as well.
Question : when we list the files using APIs inside a folder in Shared Drive (Team Drive), how to detect who created the file ?
You can use the Revisions API
So, if you list the revisions of a file specifying revisions/lastModifyingUser, revisions/id in parameter fields, you can retrieve the changes of the file and the corresponding user who modified the file.
Thereby, the first revision ("id": "1") should correspond to the creation / upload of the file and the related lastModifyingUser is the creator of the file.
Additional information:
Listing files, will return a files resource with the properties as specified here - The resource has properties like "owners" or "lastModifyingUser", but unfortunately not "creator"
There is already a feature request asking to implement the option to retrieve the user who added a file to a shared drive. - You can "star" it to increase visibility and thus hopefully sooner implementation
Until then unfortunately you cannot retrieve the file creator without the workaround with the Revisions API

Google Drive API - Recovering the original folder for a file restored from trash before the original folder has been restored

Scenario
There are many files in a Google (Team) Drive, all of which are nested in folders
All of these folders and their files are deleted by mistake.
In an attempt to recover the data, the Restore button on the Web UI is used
The Restore button in the Web UI does not seem to care about the order, and restores files before restoring the folders that contained them.
Thousands of files end up in the root directory of the drive since their folders "don't exist" when being restored from Trash.
What I need:
How can I recover the information about where the files used to reside, in order to patch this correctly?
What I tried:
Get all of the revisions for one of the files in question with Revisions: list and see there is only 1 result.
Get the revision information for that revision with Revisions: get
Realise that the revision information contains nothing about parents.
Get the file information with Files: get
Confirm that the head revision meta matches the only revision found in step 1
See that the file meta shows the parent is the root.
I can't find a way of determining what the original folder was for the trashed and incorrectly restored file.
After further investigation I don't think it is possible to recover the information about the previous parents from the Google Drive API v3.
However, if you are using Google Team Drive, as part of the G Suite, you should have access to the G Suite Admin SDK Reports API unless you are on a G Suite Basic account. With that you can use Activities: list with various filters to pull back the information about where the orphaned files came from.
I have published my solution here: Google Team Drive Orphaned File Restorer
This console application can pull a list of Google Drive file movements between two points in time, optionally filtered by the IP Address of the user who made the changes (i.e. restored the files from trash). It will then use that information to re-parent the files in question. You can do a dry run to see what the changes will be before you actually commit.

Lifetime of a Google Apps script with no owner?

I have a Google Apps script that belonged to a former colleague. They have been offboarded and the script currently has No Owner. I am curious how long this GScript will exist without an owner assigned.
The user has been deleted since May and we have a backup.
Based on the fact that you can restore a user within the first 20 days of being deleted and a file can be restored within the first 25 days of being deleted (if the user still exist) I would say it will remain available 20 days. After that it will be permanently deleted and cannot be restored or transfer, I would suggest to make a copy of the file.
You can check these G Suite Administrator Help Center Articles to have more details about how Google manage deleted users and files.
Restore a recently deleted user
Restore deleted files or shared drives
Restore a deleted user's Drive files
UPDATE:
After checking the image well, I can see that the file is within a Shared Drive or "Team Drive" if that is the case according to "Store & share files with shared drives".
Members of a shared drive share ownership of any files and folders.
If someone leaves the shared drive, any files they added will stay.
You can still share files with a link or invite.
That means if the user is deleted (leaves the share drive) the file added will stay, so it will be forever. And the Shared Drive cannot be deleted if contains files, if someone wants to delete the Shared Drive they must delete the files first.
I just wanted to follow this up as I found a bug on Google Issue Tracker which shows that if a user is deleted while they own some scripts that are bound to Drive files, upon user deletion even if the Drive files are transferred to another user on the domain the owner doesn't change and the scripts can become inaccessable. Make sure that in the future all scripts are copied or saved in a shared Drive so that they can be accessed on user account deletion/migration.

User with edit privileges made files I own not viewable by me

I shared a folder containing files and subfolders with files with multiple users and gave them edit permissions. One users account has modified the viewing ability(?) of every file and subfolder they had access to. From my account when I open the top level folder I see it empty but I know these files are still there because I own the majority of them and my drive storage amount hasn't changed.
The user claims they have no idea how it happened so maybe that it was an app they unknowingly gave drive access to?
Is there any way I can fix this from the Google Drive web interface? I found Google Apps Scripts has access to the Drive API, should I find a script that can mass change the permissions?
Only the owner can delete the file.
The files were removed from the folder but were not deleted.
Therefore, they take up disk space.
To view files outside the folder, follow these steps:
https://drive.google.com/drive/search?q=is:unorganized%20owner:me
Excuse my bad English
Venca

add prefix to all files and folders in google drive

How can I add a prefix or a suffix to all files/ folders in Google Drive using Google Script. The objective is that whenever a users needs to be deleted to add a suffix to all files and folders and transfer to another user so that the documents of the user can easily be tracked at a later stage.
Regards
The File.setName() method in the Drive Service will do what you want. I'd be happy to help further if you post a sample of your code.