Google Drive resumable upload in v3

Viatorus :

I am looking for some help/example to perform a resumeable upload to Google Drive using the new v3 REST API in Java.

I know there is a low level description here: Upload files | Google Drive API. But at the moment I am not willing to understand any of these low level requests, if there isn't another, simpler method ( like former MediaHttpUploader, which is deprecated now...)

What I currently do is:

    File fileMetadata = new File();
    fileMetadata.setName(name);
    fileMetadata.setDescription(...);
    fileMetadata.setParents(parents);
    fileMetadata.setProperties(...);
    FileContent mediaContent = new FileContent(..., file);
    drive.files().create(fileMetadata, mediaContent).execute();

But for large files, this isn't good if the connection interrupts.

Dimas Mendes :

I've just created an implementation on that recently. It will create a new file on your DriveFolder and return its metadata when the task succeeds. While uploading, it will also update the listener with uploading info. I added comments to make it auto explanable:

public Task<File> createFile(java.io.File yourfile, MediaHttpUploaderProgressListener uploadListener) {
        return Tasks.call(mExecutor, () -> {
            //Generates an input stream with your file content to be uploaded
            FileContent mediaContent = new FileContent("yourFileMimeType", yourfile);
            //Creates an empty Drive file           
            File metadata = new File() 
                    .setParents(parents) 
                    .setMimeType(yourFileMimeType)
                    .setName(yourFileName);

            //Builds up the upload request
            Drive.Files.Create uploadFile = mDriveService.files().create(metadata, mediaContent);
            //This will handle the resumable upload
            MediaHttpUploader uploader = uploadBackup.getMediaHttpUploader();
            //choose your chunk size and it will automatically divide parts
            uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE); 
            //according to Google, this enables gzip in future (optional)
            uploader.setDisableGZipContent(false);  versions
            //important, this enables resumable upload
            uploader.setDirectUploadEnabled(false); 
            //listener to be updated
            uploader.setProgressListener(uploadListener);

            return uploadFile.execute();
        });
    }

And make your Activity extends MediaHttpUploaderProgressListener so you have real time updates on the file progress:

@Override
public void progressChanged(MediaHttpUploader uploader) {
    String sizeTemp = "Uploading"
            + ": "
            + Formatter.formatShortFileSize(this, uploader.getNumBytesUploaded())
            + "/"
            + Formatter.formatShortFileSize(this, totalFileSize);
    runOnUiThread(() -> textView.setText(sizeTemp));
}

For calculating the progress percentage, you simply do:

double percentage = uploader.getNumBytesUploaded() / totalFileSize

Or use this one:

uploader.getProgress()

It gives you the percentage of bytes that have been uploaded, represented between 0.0 (0%) and 1.0 (100%). But be sure to have your content length specified, otherwise it will throw IllegalArgumentException.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141455&siteId=1