我在给定的一个(FooFolder)中创建一个文件夹(新文件夹)。如果我使用"writer"作为权限角色,一切都ok
My Drive
|->FooFolder
|->NewFolder
我有这三个函数:
function fileCreation($service, $fileName, $emailToOwn, $type )
{
//Create the file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($fileName);
$file->setMimeType($type);
$file = $service->files->insert( $file );
$permission = insertPermission($service, $file->getId(),$emailToOwn,"user","writer");
return $file;
}
function insertPermission($service, $fileId, $value, $type, $role) {
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setValue($value);
$newPermission->setType($type);
$newPermission->setRole($role);
return $service->permissions->insert($fileId, $newPermission, array(NULL,false));
}
function insertFileIntoFolder($service, $folderId, $fileId) {
$newChild = new Google_Service_Drive_ChildReference();
$newChild->setId($fileId);
try {
return $service->children->insert($folderId, $newChild);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return false;
}
当我将权限角色更改为"Owner"时,新文件夹既在父目录中正确创建,又显示在根文件夹中。
调用后指向我的驱动器图片的链接服务->权限->插入美元($文件标识,"所有者");
调用后指向我的驱动器图片的链接服务->权限->插入美元($文件标识,"作家");
我找到了一个解决方案
它可以从我创建的每个文件夹中删除父文件夹(我从Drive-Api文档中获得代码)
/**
* Remove a root from a folder.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param String $folderId ID of the folder to remove the file from.
* @param String $fileId ID of the file to remove from the folder.
*/
function removeRootFromFolder($service, $fileId) {
try {
$parents = $service->parents->listParents($fileId);
foreach ($parents->getItems() as $parent) {
if ($parent->getIsRoot())
{
$service->parents->delete($fileId, $parent->getId());
}
}
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
更新了将文件放入文件夹
的方法/**
* Insert a file into a folder.
*
* @param Google_Service_Drive $service Drive API service instance.
* @param String $folderId ID of the folder to insert the file into.
* @param String $fileId ID of the file to insert.
* @return Google_Service_Drive_ChildReference The inserted child. NULL is
* returned if an API error occurred.
*/
function insertFileIntoFolder($service, $folderId, $fileId) {
$newChild = new Google_Service_Drive_ChildReference();
$newChild->setId($fileId);
try {
$r = $service->children->insert($folderId, $newChild);
removeRootFromFolder($service, $fileId);
return $r;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return false;
}