[英]How to copy a google drive document from one folder to another using JavaScript API
I have two folders Folder1
and Folder2
in my Google drive.我的 Google 驱动器中有两个文件夹Folder1
和Folder2
。
I have created a google drive document doc1
in Folder1.我在 Folder1 中创建了一个谷歌驱动器文档doc1
。 Later i need to copy this document to Folder2.稍后我需要将此文档复制到Folder2。 How can i achieve this.我怎样才能做到这一点。
I tried the below link but its creating the copy on the same folder.我尝试了下面的链接,但它在同一个文件夹中创建了副本。
https://developers.google.com/drive/v2/reference/files/copy https://developers.google.com/drive/v2/reference/files/copy
/**
* Copy an existing file.
*
* @param {String} originFileId ID of the origin file to copy.
* @param {String} copyTitle Title of the copy.
*/
function copyFile(originFileId, copyTitle) {
var body = {'title': copyTitle};
var request = gapi.client.drive.files.copy({
'fileId': originFileId,
'resource': body
});
request.execute(function(resp) {
console.log('Copy ID: ' + resp.id);
});
}
As I have read on this blog , there is no direct method to move files from one folder to another in Google Drive.正如我在此博客上所读到的,没有直接的方法可以将文件从 Google Drive 中的一个文件夹移动到另一个文件夹。 Your option is to copy a file to another folder, set its name to the original file and then delete the original using the setTrashed(true) method of File.您的选择是将文件复制到另一个文件夹,将其名称设置为原始文件,然后使用 File 的setTrashed(true) 方法删除原始文件。
Be noted that this method will fail if the file/s have been uploaded by another user while the script is running under a different user.请注意,如果脚本在其他用户下运行时文件已由其他用户上传,则此方法将失败。
Here is a sample snippet:这是一个示例片段:
function copyFile(originFileId, destinationFileId) {
var files = originFileId.getFiles();
while (files.hasNext()) {
var file = files.next();
file.makeCopy(target).setName(file.getName());
file.setTrashed(true);
}
Hope this helps!希望这可以帮助! :) :)
Hackaround in 2020. Go to Google Colab and use ! cp path1 path2
2020 年的 Hackaround。转到 Google Colab 并使用! cp path1 path2
! cp path1 path2
You can even use -r option for directories.您甚至可以对目录使用 -r 选项。 Pretty neat!漂亮整齐! Though I should warn you this method is slow if your folder contains a lot of files or contains large files.尽管我应该警告您,如果您的文件夹包含大量文件或包含大文件,则此方法会很慢。
Edit: typo in Colab编辑:Colab 中的错字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.