使用Java中的服务帐户将数据上传到共享驱动器 [英] Uploading data to shared drive using service account in java

查看:103
本文介绍了使用Java中的服务帐户将数据上传到共享驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java将文件上传到共享驱动器. 我已经创建了一个服务帐户,并与该服务帐户的地址共享了共享驱动器(名称为DB-Drive).

I want to upload the files to shared drive using java. I have created a service account and I have shared the shared-drive (name is DB-Drive) with the service account's address.

全局变量

private static HttpTransport httpTransport;
private static Drive drive;

主要功能(文件的初始化和列出)

Main function (initialize and listing of the files)

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    Credential credential = authorize();
    drive = new Drive.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential).setApplicationName(
                    "Quickstart").build();

    uploadFile();

    FileList result = drive.files().list().execute();
    List<File> files = result.getFiles();
    if (result == null || result.isEmpty()) {
        System.out.println("No files found.");
      } else {
        System.out.println("Files:");
        for (File file : files) {
             System.out.printf("%s %s \n", file.getName(),file.getId());
                }
            }
        } 

授权码

        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("resources/credentials.json"))
                .createScoped(Collections.singleton(DriveScopes.DRIVE_FILE));

        return credential;

上传代码

 String folderId = "***DB-Drive id****";
        File fileMetadata = new File();
        fileMetadata.setName("photo.jpg");
        fileMetadata.setParents(Collections.singletonList(folderId));
        java.io.File filePath = new java.io.File("/home/*****/Downloads/qck_1552369371.jpeg");
        FileContent mediaContent = new FileContent("image/jpeg", filePath);
        File file = drive.files().create(fileMetadata, mediaContent)
                .setFields("id, parents")
                .execute();
        System.out.println("File ID: " + file.getId());
        return file;

我将TeamDrive或共享驱动器的DB-Drive设置为上载文件的父文件,但仍然无法在共享驱动器文件夹中查看.

I am setting DB-Drive which is the teamdrive or the shareddrive as the parent of the uploaded file but still I am not able to view this in the shared drive folder.. Please help me with this.

我要添加完整的流程:-

I am adding the complete flow :-

1.)我使用 https:中提到的步骤创建服务帐户: //developers.google.com/identity/protocols/oauth2/service-account 我选择了该项目,并提供了说明和所有内容.

1.) I create the service account using the steps mentioned at https://developers.google.com/identity/protocols/oauth2/service-account I selected the project and gave the description and everything.

2.)我没有将域级别委派给服务帐户,而是与服务帐户地址共享了DB驱动器(团队驱动器)(在credit.json文件中提到为client_email *****.gserviceaccount .com)

2.) I did'nt gave domain level delegation to service account rather I shared the DB-drive(team drive) with service account address (which was mentioned in credentials.json file as client_email *****.gserviceaccount.com)

3.)然后,我使用上面的代码上传文件,要求服务帐户通过将DB-Drive的ID指定为文件的父ID来将文件上传到共享驱动器(即DB-Drive).

3.) Then I used the above code to upload the file asking service account to upload the file to shared drive i.e DB-Drive by specifying the DB-Drive's id as the parent id of the file.

4.),但文件仍被上传到服务帐户的驱动器,但未链接到共享驱动器.

4.) but still the file is getting uploaded to service account's drive it is not getting linked to shared drive.

5.)DB-Drive是父文件夹(唯一的文件夹).

5.) DB-Drive is the parent folder (the only folder).

任何潜在客户都会有很大帮助.

Any leads would be a great help.

推荐答案

@ziganotschka指出,如果在共享驱动器中创建文件,则需要将supportsAllDrives设置为true.不建议使用Java客户端的setSupportsTeamDrives选项.而是使用

As @ziganotschka points out you need to set supportsAllDrives to true if creating a file in a shared drive. The Java client's setSupportsTeamDrives option is deprecated. Instead use setSupportsAllDrives to true.

  • 解决问题
  • 问问自己-问题与服务帐户的使用或其他原因有关吗?
  • 为此,在没有服务帐户的情况下测试相同的请求
  • 如果服务帐户原来是罪魁祸首
    • 验证您是否已在GCP控制台中正确启用了域范围委派
    • 验证您是否在管理控制台中为委托的服务帐户提供了正确的作用域
    • Isolate the problem
    • Ask yourself - is the problem related to the usage of the service account or something else?
    • For this, test the same request without a service account
    • If the service account turns out to be the culpit
      • Verify that you enabled domain-wide delegation correctly in the GCP console
      • Verify that you provided the correct scopes to the delegated service account in the Admin console
      • 如果问题与编码有关,请调试代码
      • 如果问题与请求有关,请根据错误采取行动,尝试使用此API 返回您
      • If the issue is coding related - debug your code
      • If the issue is request related, act according to the error the Try this API returns you
      • 文件位于共享驱动器上(在这种情况下,您必须如上所述将supportsAllDrives设置为true)
      • 或fileId不正确
      • 或者您用于身份验证的帐户无法访问该文件
      • the file is located on a shared Drive (in this case you have to set supportsAllDrives to true as mentioned above)
      • or the fileId is not correct
      • Or the file is not accessible to the account you use for authentication

      这篇关于使用Java中的服务帐户将数据上传到共享驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆