I wish to upload files from my java application to the shared google drive link provided by the end user. End user has allowed 'can edit' permission to the google drive folder that is shared. I do not see any API in Google drive that helps me in uploading files to the user's shared google drive link. When this shared link is accessed from the browser, it shows the web page that shows list of files under folder mapped thru the shared link and it allows me to drag drop file on the blank area to upload. As this link is the web page, I can't use from java application hence looking for similar API.
2 Answers
The Google Drive API is documented at https://developers.google.com/drive/v3/web/quickstart/java
Typically, you will upload the document into your own root folder ("My Drive") in Google Drive and then move or add the document to the target folder shared by the other user.

- 857
- 5
- 11
As per this documentation, the supportsAllDrives=true
parameter informs Google Drive that your application is designed to handle files on shared drives. But it is also mentioned that the supportsAllDrives
parameter will be valid until June 1, 2020. After June 1, 2020, all applications will be assumed to support shared drives. So I tried with the Google Drive V3 Java API, and found that shared drives are currently supported by default in the execute
method of Drive.Files.Create
class of V3 APIs. Attaching a sample code snippet for your reference. This method uploadFile
uploads a file to Google drive folder using resume-able upload and returns the uploaded fileId.
public static String uploadFile(Drive drive, String folderId , boolean useDirectUpload) throws IOException {
/*
* drive: an instance of com.google.api.services.drive.Drive class
* folderId: The id of the folder where you want to upload the file, It can be
* located in 'My Drive' section or 'Shared with me' shared drive with proper
* permissions.
* useDirectUpload: Ensures whether using direct upload or Resume-able uploads.
* */
private static final String UPLOAD_FILE_PATH = "photos/big.JPG";
private static final java.io.File UPLOAD_FILE = new java.io.File(UPLOAD_FILE_PATH);
File fileMetadata = new File();
fileMetadata.setName(UPLOAD_FILE.getName());
fileMetadata.setParents(Collections.singletonList(folderId));
FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);
try {
Drive.Files.Create create = drive.files().create(fileMetadata, mediaContent);
MediaHttpUploader uploader = create.getMediaHttpUploader();
//choose your chunk size and it will be automatically divided parts
uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
//As per Google, this enables gzip in future (optional) // got from another post
uploader.setDisableGZipContent(false);
//true enables direct upload, false resume-able upload
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setProgressListener(new FileUploadProgressListener());
File file = create.execute();
System.out.println("File ID: " + file.getId());
return file.getId();
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}

- 177
- 3
- 16