简体   繁体   English

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

[英]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.

相关问题 在Google App Engine Flex上运行时,使用Java将文件从Google存储桶移动到Google云端硬盘 - Move file from Google Storage Bucket to Google Drive using Java while running on the Google App Engine Flex 通过Google App Engine(Java)将文件上传到Google云端存储 - Upload file to Google cloud storage from Google App Engine (Java) 如何使用javaGUI(java应用程序)将文件上传到Google App Engine - How to upload a file by using javaGUI(java application) to google app engine 使用java api将文件上传到谷歌驱动器 - upload file into google drive using java api 从java将本地文件上传到Google驱动器 - Upload local file into Google drive from java 从 App Engine 应用程序将文件上传到 Google Drive - Upload files to Google Drive from App Engine application 通过Google App Engine(JAVA)将文件(图像或视频)从JSP上传到Google云存储 - Upload File (image or video) from JSP to google cloud storage via Google app engine(JAVA) 从Java Google App引擎(Servlet)获取访问Google电子表格(保存到Google云端硬盘帐户中) - Get access google spreadsheet (saved into google drive account) from a java google app engine (servlet) 如何使用 Java Google Drive API 上传文件 - How to upload file using Java Google Drive API 使用 Java 程序将文件上传到 Google Drive 的共享链接 - File Upload to Shared Link of the Google Drive using Java program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM