簡體   English   中英

Google雲端硬盤API設置文件夾的父級

[英]Google Drive api setting folder parents

我一直在嘗試使用google 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不會復制文件,而是設置符號鏈接。 從根目錄刪除該文件夾也會從父文件夾中刪除它。

真正的問題很簡單:我想念什么?

在函數“ insertFileToFolder”中,而不是使用parents-> insert函數,而是使用children-> insert('FolderID','newChild')

您可以在以下鏈接上看到確切的實現: https : //developers.google.com/drive/v2/reference/children/insert#examples

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM