Search code examples
phpgoogle-drive-apigoogle-php-sdk

Upload File to Teamdrives with Drive API and PHP


I want to upload files to my teamdrive but it fail. Upload to my drive works.

I call the function with a local file, array with folder id in my Teamdrive and the Team Drive ID. $service a Google_Service_Drive Object and $client a Google_Client

I use the option supportsTeamDrives.

If I try listFiles the Teamdrives also not exist.

How can I acces Teamdrives over the API in PHP?

This Version works now:

function uploadGD($local_file, $folderid = NULL, $teamdrive = NULL)
{
    global $service;
    global $client;
    try {

    // Call the API with the media upload, defer so it doesn't immediately return.
        $client->setDefer(true);
        //$request = $service->files->create($file);

        $optParams = array(
            'fields' => 'id',
            'supportsTeamDrives' => true,
        );

            $request = $service->files->create(new Google_Service_Drive_DriveFile(array(
            "name" => basename($local_file),
            "teamDriveId" => $teamdrive,
            "parents" => $folderid,
            "mimeType" => mime_content_type($local_file))), $optParams);

        // Create a media file upload to represent our upload process.
        $media = new Google_Http_MediaFileUpload(
          $client,
          $request,
          mime_content_type($local_file),
          null,
          true,
          1 * 1024 * 1024
        );
        $media->setFileSize(filesize($local_file));

        // Upload the various chunks. $status will be false until the process is
        // complete.
        $status = false;
        $handle = fopen($local_file, "rb");
        while (!$status && !feof($handle)) {
          $chunk = fread($handle, $chunkSizeBytes);
          $status = $media->nextChunk($chunk);
         }

        // The final value of $status will be the data from the API for the object
        // that has been uploaded.
        $result = false;
        if($status != false) {
          $result = $status;
        }

        fclose($handle);
        // Reset to the client to execute requests immediately in the future.
        $client->setDefer(false);

        return "google|" . $result["id"];
    } catch (Exception $e) {
            return "Fehler:".$e->getMessage();
    }

}

The Error Message shows:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: 0AHUD0ou-txfUUk9PVA.",
    "locationType": "parameter",
    "location": "fileId"
   }
  ],
  "code": 404,
  "message": "File not found: 0AHUD0ou-txfUUk9PVA."
 }
}

Solution

  • "File not found: 0AHUD0ou-txfUUk9PVA.",

    basically means that the user you are authenticating with does not have access to the file in question there for can not find it. You should do a files.list in order to see which files a user has access to.

    If you are authenticating with a service account you need to make sure that the service account has been granted access to the team drive account then it will be able to access the files.