谷歌云端硬盘 api 设置文件夹父级


Google Drive api setting folder parents

我一直在尝试使用谷歌api首先创建一个根文件夹,然后在该根文件夹中创建子文件夹。 然而,结果并不令人鼓舞。 以下是创建文件夹的代码:

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;
    }

然而,每当我运行这个脚本时,父母都没有设置。 相反,子文件夹位于驱动器中,与父文件夹并列。

我也尝试像这样手动设置父标志:

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;
    }

这确实有效...有点。 它不会将子文件夹"移动"到父文件夹,而是复制它们或创建某种符号链接,并且文件夹似乎位于父文件夹下。

编辑:我已经确认插入文件到文件夹不会复制文件,而是设置符号链接。 从根目录中删除文件夹也会从父文件夹中删除它。

真的是简单的问题:我错过了什么?

在函数"insertFileToFolder"中,不要使用parent->insert函数,而是使用children->insert('FolderID', 'newChild')

您可以在此链接上看到确切的实现:https://developers.google.com/drive/v2/reference/children/insert#examples