google-api-go-client icon indicating copy to clipboard operation
google-api-go-client copied to clipboard

drive: FilesCreateCall.Media does not support file size

Open dougreese opened this issue 4 years ago • 2 comments

In the v3 API, using Files.Create(...) with Media support does not allow for specifying total file size. As a result, the ProgressUpdater function supplied always receives a total value of 0.

Using ReusableMedia does support total size, but the doc states:

Deprecated: use Media instead.

The only way to calculate upload progress is to use the deprecated ReusableMedia support. The internally created MediaInfo does not expose the size value, and the only way to set it is via NewInfoFromReusableMedia, which is called by the FilesCreateCall.ReusableMedia method. FilesCreateCall.Media uses NewInfoFromMedia, and I don't see a way to specify size.

dougreese avatar Feb 17 '20 03:02 dougreese

@asrivas, any thoughts on if we should expose this?

Perhaps @broady has ideas?

tbpg avatar Feb 18 '20 15:02 tbpg

Try this.

f, _ := os.Open(*File)
info, _ := f.Stat()
total := FileSizeFormat(info.Size())

upf := srv.Files.Create(&drive.File{Name: info.Name()}).Media(f).ProgressUpdater(func(current, _ int64) {
	fmt.Printf("Uploaded %s/%s @ %.2f%%\r", FileSizeFormat(current), total, float64(current*100)/float64(info.Size()))
})
if _, err := upf.Do(); err == nil {
	log.Println("Done.")
} else {
	log.Println(err.Error())
}

du5 avatar Jun 09 '21 12:06 du5