0

I am new to the Google Drive API and have gotten to the point where I have successfully uploaded a file and subsequently am able to list out the file(s) that I uploaded. Something that confused me was that files I uploaded did NOT appear in my Google Drive until I viewed the file via the webContentLink address. Only after viewing the file in a browser via the webContentLink address did the file then magically appear in my Google Drive.

I have seen similar questions on stackoverflow but those seem to be related to the fact that a parent ID was not specified in the upload. I am just curious as to why the file doesn't appear in my Google Drive unless I view the file separately via the webContentLink link. Here is my upload code:

 function insertFile($service, $title, $description, $parentId, $mimeType, $filename)
 {
     $file = new Google_Service_Drive_DriveFile();
     $file->setName($title);
     $file->setDescription($description);
     $file->setMimeType($mimeType);
     
     // Set the parent folder.
     if ($parentId != null) {
         $parent = new ParentReference();
         $parent->setId($parentId);
         $file->setParents(array($parent));
     }
     try {
         $data = file_get_contents($filename);
         $createdFile = $service->files->create($file, array('data' => $data, 'mimeType' => $mimeType));
         
            $Permission = new \Google_Service_Drive_Permission(array(
                'type' => 'anyone',
                'role' => "reader",
                'additionalRoles' => []
                      ));
            $service->permissions->create(
                $createdFile->getId(), $Permission, array('fields' => 'id'));

         return $createdFile;
         
     } catch (Exception $e) {
         die("An error occurred: " . $e->getMessage());
     }
 } 
 
 
$title="Berkobien Tree";
$description="BerkobienTree upload";
$parentId=null;
$mimeType='application/pdf';
$filename='D:\WWWRoot\babycity.com\httpdocs\BerkobienTree.pdf';

$createdFile = insertFile($service, $title, $description, $parentId, $mimeType, $filename);
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    I cannot reproduce this behaviour. The uploaded file does show up in Drive without having to view the webContentLink. What makes you think it doesn't appear in your Drive until you view that? How are you looking for the file? Are you waiting some time before making sure it's not there? – Iamblichus Mar 02 '21 at 11:07

1 Answers1

0

it's most likely due to some latence coming from your connection. When I upload new files, or move files between drives with the API it most often appears instantly, but some other times it takes a few seconds (not more than one minute though). But if you refresh your drive's page emptying cache it should do the trick.

When I create a folder I simply give these informations :

$folderFile = new \Google_Service_Drive_DriveFile([
    'name' => $folderName,
    'mimeType' => 'application/vnd.google-apps.folder',
    'parents' => [$receivingFolderId],
    'id' => $service->files->generateIds(['count' => 1, 'space' => 'drive']),
]);
$service->files->create($folderFile, [
    'includePermissionsForView' => 'published',
    'supportsAllDrives' => true,
]);

and for creating a file / copying a file

// Create new file with seedFile's name and mimeType
$newFile = new \Google_Service_Drive_DriveFile([
    'name' => $name,
    'mimeType' => $mimeType,
    'parents' => [$PARENT_ID],
]);

// Copy the original file data into the newly created file
 $service->files->copy($originalFileId, $newFile, [
    'supportsAllDrives' => true,
]);

/*
So because I'm using a Team drive and an external drive for this process
I do not have the rights to delete a file from an external drive so I simply remove its parent, and the file is now an 'orphan'. 
It is not a suppression of the file !
In my case it fits my needs.
*/
// Remove the original file's parent folder to delete it from his folder
 $service->files->update($originalFileId, (new \Google_Service_Drive_DriveFile()), [
    'includePermissionsForView' => 'published',
    'removeParents' => $originalFileId->getParents(),
    'supportsAllDrives' => true,
]);

As you mentioned, you already can create a file, and from your code I can see that it works. So I think your problem just comes from a latence in your internet connection ? Maybe try to set the parent a different way ? I'm just speculating at this point.

Zekemaz
  • 19
  • 6