Move a folder into a Team Drive using Google Drive API

2k Views Asked by At

I'm migrating My Drive to Team Drives and I have a few thousand folders to move, each one into its own Drive. I have some php code using google/apiclient:2.7 that can use the Google Drive API to create the Drives. I now need to move the folders and am getting errors while doing so.

My code is authenticating using OAuth for a user that has the proper permissions and is able to migrate a folder using the Google UI. When I use the php code to move a file into a team drive, it moves successfully. When I try to do so with a folder I get an error message.

code:

//$files is an array where the values are the google id's
$id = $files['File adI3O'];
$parentId = $files['Drive'];

$google_file = new Google_Service_Drive_DriveFile();
//The parents field is not directly writable in update requests. Use the addParents and removeParents parameters instead.
//$google_file->setParents([$parentId]);

/** @var Google_Service_Drive $service */
$response = $service->files->update($id, $google_file, $service->optParams([
    'supportsAllDrives' => true,
    'addParents' => $parentId,
//'useDomainAdminAccess' => true,//(update) unknown parameter: 'useDomainAdminAccess'
]));

(The removeParents paramter is absent from my code. I was successful moving the file without using the removeParents paramter so it must not be needed.)

error message when setting $id to $files['Folder 69daL']:

Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "teamDrivesFolderMoveInNotSupported",
    "message": "Moving folders into shared drives is not supported."
   }
  ],
  "code": 403,
  "message": "Moving folders into shared drives is not supported."
 }
}

Google Documentation indicates that with proper permissions a Folder can be moved into a Shared Drive. How can it be done using the Google Drive API?

2

There are 2 best solutions below

0
On

I think, like the error message says, it is not supported. The documentation says that it is possible, but does not say it is possible for the API to do it. Google APIs historically have not always exposed every action that a user can do. You might be out of luck if you want to do it with the API.

https://developers.google.com/drive/api/v3/about-shareddrives

The API docs do not mention moving files into shared drives at all; only managing a shared drives's permissions.

0
On

Moving folders into shared drive is not currently supported. But you can move files from my-drive to shared-drive using API v3.

supportsAllDrives parameter needs to be True when you're trying to move files from my-drive to shared-drive or vice versa.

Here is a python example:

service.files().update(fileId='put_file_id', addParents='parent-id', removeParents="current-parent-id", supportsAllDrives=True).execute()