【问题标题】:How to create folder dynamic and upload multiples file using Java Google Drive API如何使用 Java Google Drive API 创建文件夹动态和上传多个文件
【发布时间】:2021-03-02 14:24:07
【问题描述】:

我有一个 Google 存储桶中的文件列表。我必须在 Google Drive 中创建一个文件夹并将所有文件从 Google Bucket 上传到该文件夹​​中。我正在正确上传文件,但无法将文件上传到特定文件夹。文件一一上传到云端硬盘根文件夹中,但我希望它们在特定文件夹中。

     Page<Blob> blobs = bucket.list(Storage.BlobListOption.prefix(BucketUrl));
     Drive driveService = DriveConfUtil.getDriveService(accessToken);
     for (Blob fileBlob: blobs.getValues()) {
       InputStream inputStream = new
       ByteArrayInputStream(fileBlob.getContent(BlobSourceOption.generationMatch()));
       File file = new File();
       file.setName(fileBlob.getName());
       file.setMimeType("application/mydocfolder.folder");
       File file1 = null;
       try {
         AbstractInputStreamContent streamContent = null;
         streamContent = new InputStreamContent("application/pdf", inputStream);
         file1 = driveService.files().create(file, streamContent).setFields("id").execute();

       } catch (IOException e) {

         e.printStackTrace();
       }
     }

【问题讨论】:

    标签: java google-drive-api google-cloud-storage google-cloud-datastore


    【解决方案1】:

    我知道您的脚本正确上传了文件,但它是在 Drive 主文件夹而不是您想要的文件夹中上传的。您只需在request body 中填写parents[] 参数即可轻松解决此问题。要在该参数中指定文件夹,您需要它的 ID。请记住,云端硬盘文件可以同时托管在不同的文件夹中。请随时向我寻求任何澄清。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      相关资源
      最近更新 更多