使用java将文件从谷歌应用引擎上传到谷歌驱动器

时间:2015-09-07 13:50:18

标签: java google-app-engine oauth google-drive-api

我已成功收到电子邮件中的pdf文件,但我必须将该Pdf文件存储到google drive.here是我的代码

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云端硬盘没有上传。

我已经用Google搜索了一周,我没有找到任何明确的文件,可以将文件从GAE上传到谷歌潜水。

提前感谢...

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案,首先我们需要了解GAE服务帐户,并且关联的Google帐户有独立的驱动器存储空间。

我的上述代码将该文件存储到GAE服务帐户驱动器(没有用户界面的服务帐户),因此我们关联的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