我正在尝试使用Google驱动程序API来执行简单的任务,例如
移动文件夹。
据我所知,Google Drive API无法提供跨文件夹移动文件的方法。
使用/ parent和/ children API可以指定文件的父文件夹,但它不适用于文件夹。此外,与文件关联的父属性不会将其移动到该文件夹下。它只是将父属性与文件关联起来(对我的情况来说根本没用)
答案 0 :(得分:9)
要将FILE-A从FOLDER-1移至FOLDER-2,您可以使用https://developers.google.com/drive/v2/reference/parents处的删除和添加电话来删除FOLDER-1作为父级并添加FOLDER-2。
您还可以使用更新的父阵列对文件执行修补程序。
答案 1 :(得分:9)
Drive API V3具有以下功能:
在文件夹之间移动文件
要为现有文件添加或删除父级,请使用addParents和> removeParents在files.update方法上查询参数。
这两个参数都可用于将文件从一个文件夹移动到另一个文件夹: https://developers.google.com/drive/v3/web/folder#inserting_a_file_in_a_folder
答案 2 :(得分:4)
您实际上仍然可以一步完成,只需在Javascript中设置请求:
身体必须:
body.parents = [{ 'id': parentId }];
请求应为:
var request = gapi.client.request({
'path': 'drive/v2/files/'+fileId,
'method': 'PUT',
'params': {'uploadType': 'multipart', 'alt': 'json'},
'headers': {
'Authorization': TOKEN
},
'body': body
});
然后执行:
request.execute(function(resp) { });
答案 3 :(得分:3)
以下是使用Patch和PHP client library将文件移至新文件夹的一步方法:
/**
* Move a file.
*
* @param Google_Service_Drive_DriveFile $service Drive API service instance.
* @param string $fileId ID of the file to move.
* @param string $newParentId Id of the folder to move to.
* @return Google_Service_Drive_DriveFile The updated file. NULL is returned if an API error occurred.
*/
function moveFile($service, $fileId, $newParentId) {
try {
$file = new Google_Service_Drive_DriveFile();
$parent = new Google_Service_Drive_ParentReference();
$parent->setId($newParentId);
$file->setParents(array($parent));
$updatedFile = $service->files->patch($fileId, $file);
return $updatedFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
更新:如果您要使用Drive API v3,请使用以下代码documented here:
/**
* Move a file.
*
* @param Google_Service_Drive_DriveFile $service Drive API service instance.
* @param string $fileId ID of the file to move.
* @param string $newParentId Id of the folder to move to.
* @return Google_Service_Drive_DriveFile The updated file. NULL is returned if an API error occurred.
*/
function moveFile($service, $fileId, $newParentId) {
try {
$emptyFileMetadata = new Google_Service_Drive_DriveFile();
// Retrieve the existing parents to remove
$file = $service->files->get($fileId, array('fields' => 'parents'));
$previousParents = join(',', $file->parents);
// Move the file to the new folder
$file = $service->files->update($fileId, $emptyFileMetadata, array(
'addParents' => $newParentId,
'removeParents' => $previousParents,
'fields' => 'id, parents'));
return $file;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
答案 4 :(得分:0)
我使用过这段代码:
File file = driveService.files().get(fileId).execute()
File targetFolder = driveService.files().get(folderId).execute()
ParentReference newParent = new ParentReference()
newParent.setSelfLink(targetFolder.getSelfLink())
newParent.setParentLink(targetFolder.parents[0].getSelfLink())
newParent.setId(folderId)
newParent.setKind(targetFolder.getKind())
newParent.setIsRoot(false)
def parentsList = new ArrayList<ParentReference>()
parentsList.add(newParent)
file.setParents(parentsList)
File updatedFile = driveService.files().update(fileId, file).execute()
答案 5 :(得分:0)
此示例代码使用Google Drive API v3。
Gfk = Google文件密钥(可以是文件或文件夹的ID)。
方法:
class Gfiles {
private $service;
public function setService(\Google_Client $client) : self {
$this->service = (new \Google_Service_Drive($client))->files;
return $this;
}
public function moveTo(string $gfk, string $to_gfk) : \Google_Service_Drive_DriveFile {
$body = new \Google_Service_Drive_DriveFile();
// Gets the first parent. You should further implement this if you wish a different behaviour.
$from_gfk = $this->service->get($gfk, ['fields' => 'parents'])->parents[0];
$this->service->update($gfk, $body, ['addParents' => $to_gfk]);
$this->service->update($gfk, $body, ['removeParents' => $from_gfk]);
return $this->service->get($gfk, ['fields' => 'parents']);
}
/**
* $parents_gfk expects an array like ['gfk parent 1', 'gfk parent 2'].
*/
private function updateParents(string $method, string $gfk, array $parents_gfk) : \Google_Service_Drive_DriveFile {
$body = new \Google_Service_Drive_DriveFile();
$parents = join(',', $parents_gfk);
$this->service->update($gfk, $body, [$method => $parents]);
return $this->service->get($gfk, ['fields' => 'parents']);
}
public function addParents(string $gfk, array $parents_gfk) : \Google_Service_Drive_DriveFile {
return $this->updateParents('addParents', $gfk, $parents_gfk);
}
public function removeParents(string $gfk, array $parents_gfk) : \Google_Service_Drive_DriveFile {
return $this->updateParents('removeParents', $gfk, $parents_gfk);
}
}