0

I am using google drive V3 REST api to upload a multipart file from a POST request & below is the java code

                String accessToken = "xyz";
                    Credential credential = new GoogleCredential().setAccessToken(accessToken);
                    Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                            .setApplicationName("xyz")
                            .build();
                File fileMeta = new File();
                fileMeta.setName(fileName);
                fileMeta.setMimeType("application/vnd.google-apps.file");
                if (!parentFolderId.isEmpty()) {
                    fileMeta.setParents(Collections.singletonList(parentFolderId));
                }
                //Code to upload file in GDrive
                java.io.File tmpFile = java.io.File.createTempFile("uploadFile", ".tmp");

                //Writing the file content to the new file
                InputStream in = multipartFile.getInputStream();
                FileOutputStream fos = new FileOutputStream(tmpFile);

                IOUtils.copy(in, fos);

                in.close();
                fos.close();

                tmpFile.deleteOnExit();
                FileContent mediaContent = new FileContent(null, tmpFile);
                File file = service.files().create(fileMeta, mediaContent)
                        .setFields("id")
                        .execute();

Upload is working fine but all the uploads are getting auto-converted into documents which I do not want. I see from the question Google Drive: Automatically convert files on upload? that convert=true needs to be set during insert. But this option is not available in V3.

Can someone tell me how do i disable this auto-conversion during uploads ?

Community
  • 1
  • 1
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105

1 Answers1

0

The reason for the auto-conversion was because of setting mime type in the file object because of which every uploaded file got converted to google document

fileMeta.setMimeType("application/vnd.google-apps.file");

This got resolved once mimeType wasn't set in File object while uploading

Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105