[英]Upload file from google app engine to google drive using java
I have received pdf file from email successfully but I have to store that Pdf file into google drive.here is my code我已成功从电子邮件收到 pdf 文件,但我必须将该 Pdf 文件存储到谷歌驱动器中。这是我的代码
private static void uploadFile(InputStream is) {
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setTitle(fileName);
fileMetadata.setMimeType("application/pdf");
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
AppIdentityCredential credential = new AppIdentityCredential.Builder(Arrays.asList(DriveScopes.DRIVE)).build();
// API_KEY is from the Google Console as a server API key
GoogleClientRequestInitializer keyInitializer = new CommonGoogleClientRequestInitializer("My API_key here");
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setApplicationName("My app name here")
.setHttpRequestInitializer(credential)
.setGoogleClientRequestInitializer(keyInitializer)
.build();
try {
Drive.Files.Insert in= service.files().insert(fileMetadata, new InputStreamContent("application/pdf",is));
com.google.api.services.drive.model.File retFile =in.execute();
if(retFile!=null){
System.out.println("Drive retFile:"+retFile.getTitle());
}
} catch (IOException e) {
System.out.println("Drive Error:"+e.toString());
}
}
I have checked associated account Google Drive nothing is uploaded.我检查了关联帐户 Google Drive 没有上传任何内容。
and i have googled past 1 week i didn't find any clear documentation for uploading file from GAE to google dive.我在过去 1 周内用谷歌搜索过,我没有找到任何明确的文档将文件从 GAE 上传到谷歌潜水。
thanks in advance...提前致谢...
Finally i have find the solution,first we need to understand GAE service account and associated Google account has separate drive storage.最后我找到了解决方案,首先我们需要了解 GAE 服务帐户和关联的 Google 帐户具有单独的驱动器存储。
My above code store the file to GAE service account drive (Service Account which has no UI) so it's not visible to our associated Google account.我上面的代码将文件存储到 GAE 服务帐户驱动器(没有 UI 的服务帐户),因此它对我们关联的 Google 帐户不可见。
So we need share the service account file to associated Google account.Here is my modified code所以我们需要将服务帐户文件共享到关联的 Google 帐户。这是我修改后的代码
private static void uploadFile(InputStream is) {
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setTitle(curDateWithFormat()+"-"+fileName);
fileMetadata.setMimeType("application/pdf");
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
AppIdentityCredential credential = new AppIdentityCredential.Builder(Arrays.asList(DriveScopes.DRIVE)).build();
// API_KEY is from the Google Console as a server API key
GoogleClientRequestInitializer keyInitializer = new CommonGoogleClientRequestInitializer("AIzaSyCKLTs_D2zYJepjno8OSVy-2CItYayvu0M");
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setApplicationName("TimesheetPDFParser")
.setHttpRequestInitializer(credential)
.setGoogleClientRequestInitializer(keyInitializer)
.build();
try {
Drive.Files.Insert in= service.files().insert(fileMetadata, new InputStreamContent("application/pdf",is));
com.google.api.services.drive.model.File retFile =in.execute();
if(retFile!=null){
Permission newPermission = new Permission();
newPermission.setValue("here your email id @gmail.com");
newPermission.setType("user");
newPermission.setRole("reader");//owner and writer role throws 400 BAD_REQUEST exception
service.permissions().insert(retFile.getId(), newPermission).execute();
System.out.println("Drive retFile:"+retFile.getId());
}
} catch (IOException e) {
System.out.println("Drive Error:"+e.toString());
}
}
Now you can see your file in shared account drive in shared with me option.现在您可以在与我共享选项的共享帐户驱动器中看到您的文件。
Referred from:引用自:
https://developers.google.com/drive/v2/reference/permissions/insert https://developers.google.com/drive/v2/reference/permissions/insert
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.