2

I'm trying to do an automated upload of large files to Google drive from my local computer using:

MY CODE

public final void copyFileGoogle(String fPath, String folderIDParent, String fName) throws IOException {


         Drive driveService = GoogleDriveUtils.getDriveService();

         File fileMetadata = new File();
         fileMetadata.setName(fName);
         fileMetadata.setParents(Collections.singletonList(folderIDParent));

         java.io.File filePath = new java.io.File(fPath + "\\" + fName);
         FileContent mediaContent = new FileContent("multipart/related", filePath);


         File file = driveService.files().create(fileMetadata, mediaContent)
                 .setFields("id, parents")
                 .execute();

and it seems to work fine but I can't tell if the upload is finished. I found the following on here that should tell me when it's finished:

FOUND ON STACK OVERFLOW

import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;

public class UploadProgressListener implements MediaHttpUploaderProgressListener {

    public void upLoadProgress(MediaHttpUploader mediaHttpUploader) throws IOException {
        if (mediaHttpUploader == null) return;
        switch (mediaHttpUploader.getUploadState()) {
            case INITIATION_STARTED:

                break;
            case INITIATION_COMPLETE:

                break;
            case MEDIA_IN_PROGRESS:

                break;
            case MEDIA_COMPLETE:
            //System.out.println("Upload is complete!");
            case NOT_STARTED:
            break;
        default:
            break;
        }
    }
}

Which looks great but I can't figure out how to implement it or if it will even still work with the new Google API.

I tried:

TRIED BUT DIDN'T WORK

UploadProgressListener ProgressListener;

ProgressListener.uploadProgress(file);

I get an error. It wants a MediaHttpUploader.... HELP

UPDATE : I did some testing and found "in my case" (windows platform), I would assume all but..., the program will not go on until the current upload is complete. I was looking to make sure I didn't over tax the system I was running it on so that was all I needed. If you actually need to know that it finished the upload in this case I guess you could just do a System.out.println("Complete.");

If anyone actually gets the UploadProgressListener to work I would still love to know how. Just out of curiosity.

Jerry
  • 21
  • 3
  • Your 'here' link for the example code using MediaHttpUploader doesn't appear to link anywhere, but it is possible that site will explain where MediaHttpUploader comes from. – jmarks Jan 17 '20 at 21:48
  • sorry When I said "on here" I meant on this website as in on "Stack Overflow" and what I found was the code just below it. – Jerry Jan 17 '20 at 22:17
  • Ah, I had thought you had a link that might help us see where you are in chasing this down. Since you're looking for guidance, it's good to look for a guide that covers MediaHttpUploader. This is what I found: https://googleapis.github.io/google-api-java-client/media-upload.html. – jmarks Jan 17 '20 at 22:34
  • That looks like exactly what I've been searching for. Thank you so much. I'm not sure why I couldn't find it but thank you! – Jerry Jan 17 '20 at 22:39
  • Hi! if the previous link helped you, please provide the solution as an answer so other people which have the same issue/doubt can know how to solve it – alberto vielma Jan 20 '20 at 10:45
  • I would love to say this worked but... errors all over the place. I used a lot of java.io imports being as the page says "the main classes of interest are MediaHttpUploader and MediaHttpProgressListener." and still a bunch so I tried using the google.api.clientHTTP imports. Nothing works. – Jerry Jan 21 '20 at 16:21

1 Answers1

1

There are two ways of achieving the multipart upload and show the uploading progress. One it's using the Drive API V2 and the other one is using the Drive API V3. I will show both solutions in case anyone is interested to try them both or it's using the old V2.


DriveAPI V3

Dependency

<dependency>
     <groupId>com.google.apis</groupId>
     <artifactId>google-api-services-drive</artifactId>
     <version>v3-rev20191108-1.30.3</version>
</dependency>

Java code

public void UploadMultipartFile(Drive drive, String pathname) throws IOException {
    File fileMetadata = new File();
    fileMetadata.setName("yourname.mp4");
    java.io.File mediaFile = new java.io.File(pathname);
    InputStreamContent mediaContent =
             new InputStreamContent("video/mp4",
                    new BufferedInputStream(new FileInputStream(mediaFile)));
    mediaContent.setLength(mediaFile.length());
    HttpResponse file = drive.files().create(fileMetadata, mediaContent)
            .setFields("id")
            .getMediaHttpUploader()
            .setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE)
            .setProgressListener(new CustomProgressListener())
            .setDirectUploadEnabled(false)
            .upload(new GenericUrl("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"));
    System.out.println(file.getStatusMessage());
}

Drive API V2

Dependency

<dependency>
   <groupId>com.google.apis</groupId>
   <artifactId>google-api-services-drive</artifactId>
   <version>v2-rev310-1.23.0</version>
</dependency>

Java code

public void UploadMultipartFile(Drive drive, String pathname) throws IOException {
    java.io.File file = new java.io.File(pathname);
    File fileMetadata = new File();
    fileMetadata.setTitle("video.mp4");
    fileMetadata.setDescription("description");
    fileMetadata.setMimeType("video/mp4");
    InputStreamContent mediaContent =
            new InputStreamContent("video/mp4",
                    new BufferedInputStream(new FileInputStream(file)));
    mediaContent.setLength(file.length());
    Drive.Files.Insert request = drive.files().insert(fileMetadata, mediaContent);
    request.getMediaHttpUploader()
            .setProgressListener(new CustomProgressListener())
            .setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE)
            .setDirectUploadEnabled(false);
    request.execute();
}

With the previous codes I assume you configured your service account and your credentials files in the right way in order to call the Drive API. There are several ways to build your Drive service, therefore if you feel more comfortable with another way, besides the one I used, feel free to change my code.Also, don't forget the mime type to the one your file needs.


Notice

The biggest difference between one version and the other one, it's how you send the request. In V2 you have to use the .execute(); from the Insert Class, meanwhile V3, you have to use the .upload() method from the MediaHttpUploader Class.

Docs

I checked these docs to help you:

Community
  • 1
  • 1
alberto vielma
  • 2,302
  • 2
  • 8
  • 15
  • I was using v3-rev105-1.23.0 of the same dependency. does that make a difference being that its a newer one? – Jerry Jan 22 '20 at 16:43
  • @Jerry, yes because you were using the [Drive API v3](https://developers.google.com/drive/api/v3/reference) and the example you had in your code was using the [Drive API v2](https://developers.google.com/drive/api/v2/about-sdk). If my answer helped you, please consider mark it as the correct answer. [How to accept an answer](https://stackoverflow.com/help/someone-answers) – alberto vielma Jan 22 '20 at 16:58
  • aaaah ok. I had planned on accepting the answer if I could get it to work. – Jerry Jan 22 '20 at 17:14
  • So that is not my code. I found that on here (Stack overflow) as a way I could try and couldn't figure out how to use it. My code does use API V3. Can they be mixed (declaring both dependencies) or can you only use one or the other. If so this wont work... – Jerry Jan 22 '20 at 17:23
  • From the original post: "Which looks great but I can't figure out how to implement it or if it will even still work with the new Google API." – Jerry Jan 22 '20 at 17:38
  • @Jerry, I updated my answer in order to help you to use it with V3, I also left the V2 to show the comparison in order to let other people see that there are ***two*** versions. – alberto vielma Jan 23 '20 at 09:09