0

I want to download a file from Google Drive using Drive API and there is no issue with Google Drive API or configuration. The problem is, I have deployed this application on cloud instance and when I click on the download button it takes more time to start the download. Seems it download into the cloud instance first and start receiving to me. How to avoid this waiting time ? I haven't write to any variables because it waste the memory when downloading large files. I'm using following code. Please help me.

 @RequestMapping(value = "/directDownload", method=RequestMethod.GET)
public ResponseEntity<Resource> directDownload(Principal principal,@RequestParam("fileId") String fileId,
                                               @RequestParam("fileName") String fileName)
        throws IOException {
    Drive driveService = GoogleDriveService.get(principal);
    OutputStream outputStream = new ByteArrayOutputStream();
    driveService.files().get(fileId)
            .executeMediaAndDownloadTo(outputStream);
    ByteArrayOutputStream bbe = (ByteArrayOutputStream) outputStream;

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="
            + fileName);
    responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
    return new ResponseEntity(bbe.toByteArray(), responseHeaders, HttpStatus.OK);
}
LahiruJ
  • 53
  • 8
  • You can't make it faster if you want to continue downloading it to the server and sending it to the client, instead try what is written at the end of the document: https://developers.google.com/drive/api/v3/manage-downloads *** If you want to allow a user to view a file directly in a web browser instead of through the API, use the webContentLink. You can either redirect a user to this URL, or offer it as a clickable link. The file must be either owned by or shared with the user in order to view it. *** Send your client this link and have him download it, hope this helps – Roie Beck Dec 08 '20 at 15:31
  • Thank you for the feedback. But here, the main requirement is to offer the actual file though my own server. So that we can whitelist (data free) specific IP address for our organization users. – LahiruJ Dec 10 '20 at 05:18

0 Answers0