问题描述
我希望将文件从我的Java应用程序上传到最终用户提供的共享google驱动器链接.最终用户已允许对共享的google驱动器文件夹进行可以编辑"权限.我没有在Google驱动器中看到任何API可以帮助我将文件上传到用户的共享Google驱动器链接.当从浏览器访问此共享链接时,它将显示一个网页,该页面显示了通过该共享链接映射的文件夹下的文件列表,它允许我将拖放文件拖放到空白区域以进行上传.由于此链接是网页,因此无法从Java应用程序中使用,因此正在寻找类似的API.
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.
推荐答案
按照此文档,supportsAllDrives=true
参数通知Google云端硬盘您的应用程序旨在处理共享驱动器上的文件.但是还提到supportsAllDrives
参数将在2020年6月1日之前有效.在2020年6月1日之后,将假定所有应用程序都支持共享驱动器.因此,我尝试使用Google Drive V3 Java API,发现默认情况下,V3 API类的Drive.Files.Create
类的execute
方法当前默认支持共享驱动器.随附示例代码段供您参考.此方法uploadFile
使用可恢复上传功能将文件上传到Google驱动器文件夹,并返回上传的fileId.
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;
}
这篇关于使用Java程序将文件上传到Google云端硬盘的共享链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!