How to create folder dynamic and upload multiples file using Java Google Drive API

Viewed 106

I have a list of files in a Google Bucket. I have to create a folder in Google Drive and upload all files into that folder from the Google Bucket. I am uploading the files correctly, but I am not able to upload the files into a specific folder. One by one the files are uploaded in the Drive root folder, but I want them in a particular folder.

     Page<Blob> blobs = bucket.list(Storage.BlobListOption.prefix(BucketUrl));
     Drive driveService = DriveConfUtil.getDriveService(accessToken);
     for (Blob fileBlob: blobs.getValues()) {
       InputStream inputStream = new
       ByteArrayInputStream(fileBlob.getContent(BlobSourceOption.generationMatch()));
       File file = new File();
       file.setName(fileBlob.getName());
       file.setMimeType("application/mydocfolder.folder");
       File file1 = null;
       try {
         AbstractInputStreamContent streamContent = null;
         streamContent = new InputStreamContent("application/pdf", inputStream);
         file1 = driveService.files().create(file, streamContent).setFields("id").execute();

       } catch (IOException e) {

         e.printStackTrace();
       }
     }
1 Answers

I get that your script uploads the file correctly, but it does so in the Drive main folder instead of your desired folder. You can fix that easily just by filling the parents[] parameter in the request body. To specify the folder in that parameter you would need its id. Please keep in mind that a Drive file can be hosted on different folders at the same time. Don't hesitate to ask me for any clarification.

Related