Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive api setting folder parents

I have been trying to use google api to create first a root folder and then subfolders inside that root folder. However the results have been less than encouraging. Here is the code to create folders:

public function createFolder($name, $parents = array())
    {    
        /** @var $client \Google_Client */
        $client = $this->client;
        /** @var $service \Google_Service_Drive */
        $service = $this->service;

        if (is_null($service)) {
            throw new Exception("Invalid google api service");
        }

        if (!$client instanceof \Google_Client) {
            throw new Exception("Invalid google api client");
        }

        if (!isset($_SESSION['access_token'])) {
            throw new Exception("No access token found");
        }

        if (!$client->getAccessToken()) {
            throw new Exception("Could not find access token in client");
        }

        $folder = new \Google_Service_Drive_DriveFile();
        $folder->setTitle($name);
        $folder->setMimeType('application/vnd.google-apps.folder');

        if (!empty($parents)) {
            echo "Setting parents of $name to: " . implode(',', $parents);
            $folder->setParents($parents);
        } else {
            echo "\n No parent ids found \n";
        }


        echo "\n Creating new folder: {$name} \n";

        $output = $service->files->insert($folder, array(
            'mimeType' => 'application/vnd.google-apps.folder',
        ));

        return $output;

    }

Yet whenever I run this script the parents are not set. Instead the subfolders are in a the drive alongside the parent folder.

Also I tried to set the parent flags manually like so:

public function insertFileToFolder($childId, $parentId)
    {
        if (empty($childId) || empty($parentId)) {
            throw new Exception("Invalid parameter");
        }

        $service = $this->getService();

        try {

            echo "\n Trying to set subfolders \n";
            $parentReference = new \Google_Service_Drive_ParentReference();
            $parentReference->setId($parentId);

            return $service->parents->insert($childId, $parentReference);

        } catch (Exception $ex) {
            echo $ex->getMessage();
        }

        return null;
    }

Which does work... kind of. It doesn't "move" the subfolders to the parent but instead either copies them or creates some kind of symlink and the folders appear to be under the parent folder.

Edit: I've confirmed that the insertFileToFolder does not copy the files but instead sets symlinks. Deleting the folder from root also deletes it from parent folder.

Simple question really: What am I missing?

like image 653
Tomkarho Avatar asked May 10 '26 14:05

Tomkarho


1 Answers

In your function 'insertFileToFolder', instead of using the parents->insert function, use children->insert('FolderID', 'newChild')

You can see an exact implementation on this link: https://developers.google.com/drive/v2/reference/children/insert#examples

like image 181
Rivero Avatar answered May 12 '26 05:05

Rivero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!