Search code examples
google-drive-apigoogle-cloud-storagestoragegoogle-drive-shared-drive

copy file in google drive using pydrive or google drive api


function copyFolderContents_(source, target) {
  // Iterate files in source folder
  const filesIterator = source.getFiles()
  while (filesIterator.hasNext()) {
    const file = filesIterator.next()

    // Make a copy of the file keeping the same name
    file.makeCopy(file.getName(), target)        // need to find python function replacing THIS LINE!!!!!!!!!!!!!!!!!!
  }
}

const toCopy = DriveApp.getFolderById('')
const copyInto = DriveApp.getFolderById('')
const newFolder = copyInto.createFolder(toCopy.getName())
copyFolderContents_(toCopy, newFolder)

I trying to rewrite this app script into python, which only copies the file but not the folder into another locatioN

is there any pydrive or vanilla HTTP restful API that could replace file.makeCopy(file.getName(), target) ?

After visiting ref , seems this restful API doesn't have a target that I could specified.


Solution

  • The parent[] parameter in the request body of Files:copy is the same as the destination in makeCopy(name, destination).

    Example:

    Here I have a folder named 68759008 which has the sample document:

    enter image description here

    and here is the destination folder I want my sample document to be copied.

    enter image description here

    Using API Explorer:

    Request Parameter:

    enter image description here

    In the request parameter, I only inserted the file ID of the sample document

    Request Body:

    {
      "parents": [
        "Insert Destination folder ID here"
      ],
      "name": "68759008_TEST_DOC_COPY"
    }
    

    Response:

    enter image description here

    Output:

    enter image description here

    There are two options to obtain the parent ID or the destination folder ID:

    • You can go to the destination folder and copy the string after the https://drive.google.com/drive/folders/
    • Use Files:list, find the file name and the mimeType should be application/vnd.google-apps.folder

    Files:list Example:

    enter image description here