![](/img/trans.png)
[英]Move file from Google Storage Bucket to Google Drive using Java while running on the Google App Engine Flex
[英]Upload file from google app engine to google drive using java
我已成功從電子郵件收到 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());
}
}
我檢查了關聯帳戶 Google Drive 沒有上傳任何內容。
我在過去 1 周內用谷歌搜索過,我沒有找到任何明確的文檔將文件從 GAE 上傳到谷歌潛水。
提前致謝...
最后我找到了解決方案,首先我們需要了解 GAE 服務帳戶和關聯的 Google 帳戶具有單獨的驅動器存儲。
我上面的代碼將文件存儲到 GAE 服務帳戶驅動器(沒有 UI 的服務帳戶),因此它對我們關聯的 Google 帳戶不可見。
所以我們需要將服務帳戶文件共享到關聯的 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());
}
}
現在您可以在與我共享選項的共享帳戶驅動器中看到您的文件。
引用自:
https://developers.google.com/drive/v2/reference/permissions/insert
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.