Search code examples
javagoogle-apigoogle-drive-apigoogle-api-java-client

Google Drive API v3- create a folder inside a folder in Java


I'm trying to develop a method in Java that enables creating a folder inside of a specific folder in Google Drive, but what I found in Google documentation (https://developers.google.com/drive/api/v3/folder) is creating only a folder or moving a file to a folder. Any help would be appreciated!


Solution

  • Just take what the API gave you for creating a folder and inserting a file in a folder and combine them.

    From the API site: 'The parents property can be used when creating a folder as well to create a subfolder.'

    String folderId = "folderID";
    File fileMetadata = new File();
    fileMetadata.setName("Invoices");
    fileMetadata.setParents(Collections.singletonList(folderId));
    fileMetadata.setMimeType("application/vnd.google-apps.folder");
    
    File file = driveService.files().create(fileMetadata)
    .setFields("id, parent")
    .execute();
    System.out.println("Folder ID: " + file.getId());