google-api-go-client
google-api-go-client copied to clipboard
drive: FilesCreateCall.Media does not support file size
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.
@asrivas, any thoughts on if we should expose this?
Perhaps @broady has ideas?
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())
}