D.A
D.A

Reputation: 23

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!

Upvotes: 2

Views: 4427

Answers (3)

Viktor Reinok
Viktor Reinok

Reputation: 130

In case you want to create a folder in a shared drive you have to specify following:

  • drive id (to file metadata)

  • supports all drives (to the files().create().setFields in builder chain)

    private String createFolder(String folderName, String parent) throws IOException {
    
         System.out.println("Creating folder " + folderName + " in " + parent);
    
         File fileMetadata = new File();
         fileMetadata.setParents(Arrays.asList(parent));
         fileMetadata.setName(folderName);
         fileMetadata.setDriveId(DRIVE_ID);
         fileMetadata.setMimeType("application/vnd.google-apps.folder");
    
         File file = drive.files().create(fileMetadata)
                            .setSupportsAllDrives(true)
                            .setFields("id")
                            .execute();
    
         System.out.println("created folder ID: " + file.getId());
    
         return file.getId();
    }
    

Upvotes: 0

SkyWalker
SkyWalker

Reputation: 29168

You need to create a drive service first. Then you can call createSubFolder method.

    // Build a new authorized API client service.
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
            .setApplicationName(APPLICATION_NAME)
            .build();

Resource Link: https://developers.google.com/drive/api/v3/quickstart/java

Here, It is required to give id of parent folder. And have to give the sub folder name.

public String createSubFolder(String parentFolderId, String subFolderName) {
    System.out.println("Sub Folder Name: "+subFolderName);
    File file = null;
    try {
        File fileMetadata = new File();
        fileMetadata.setName(subFolderName);
        fileMetadata.setMimeType("application/vnd.google-apps.folder");
        if (parentFolderId != null) {
            List<String> parents = Arrays.asList(parentFolderId);
            fileMetadata.setParents(parents);
        }
        file = driveService.files().create(fileMetadata).setFields("id, name").execute();
        System.out.println("Folder ID: " + file.getId());
        return file.getId();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Upvotes: 0

Jaybro90
Jaybro90

Reputation: 104

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());

Upvotes: 2

Related Questions