gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:57:51 24 4
gpt4 key购买 nike

我已成功从电子邮件中收到 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 云端硬盘,未上传任何内容。

过去 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

关于java - 使用java将文件从谷歌应用引擎上传到谷歌驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com