简体   繁体   English

如何使用 Java Google Drive API 创建文件夹动态和上传多个文件

[英]How to create folder dynamic and upload multiples file using Java Google Drive API

I have a list of files in a Google Bucket.我有一个 Google 存储桶中的文件列表。 I have to create a folder in Google Drive and upload all files into that folder from the Google Bucket.我必须在 Google Drive 中创建一个文件夹并将所有文件从 Google Bucket 上传到该文件夹中。 I am uploading the files correctly, but I am not able to upload the files into a specific folder.我正在正确上传文件,但无法将文件上传到特定文件夹。 One by one the files are uploaded in the Drive root folder, but I want them in a particular folder.文件一一上传到 Drive 根文件夹中,但我希望它们在特定文件夹中。

     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();
       }
     }

I get that your script uploads the file correctly, but it does so in the Drive main folder instead of your desired folder.我知道您的脚本正确上传了文件,但它是在 Drive 主文件夹而不是您想要的文件夹中这样做的。 You can fix that easily just by filling the parents[] parameter in the request body .您只需在请求正文中填写parents[]参数即可轻松解决此问题。 To specify the folder in that parameter you would need its id.要在该参数中指定文件夹,您需要它的 ID。 Please keep in mind that a Drive file can be hosted on different folders at the same time.请记住,云端硬盘文件可以同时托管在不同的文件夹中。 Don't hesitate to ask me for any clarification.不要犹豫,问我任何澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM