Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push: customizable upload chunk size #840

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ default count of 20:
drive push -retry-count 4 a/bc/def terms
```

* You can also specify the upload chunk size to be used to push each file, by using flag
`-upload-chunk-size` whose value is in bytes. If you don't specify this flag, by default
the internal Google APIs use a value of 8MiB from constant `googleapi.DefaultUploadChunkSize`.
Please note that your value has to be a multiple of and atleast the minimum upload chunksize
of 256KiB from constant `googleapi.MinUploadChunkSize`. See https://godoc.org/google.golang.org/api/googleapi#pkg-constants

### End to End Encryption

See [Issue #543](https://github.com/odeke-em/drive/issues/543)
Expand Down
7 changes: 5 additions & 2 deletions cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,9 @@ type pushCmd struct {
ExponentialBackoffRetryCount *int `json:"retry-count"`
EncryptionPassword *string `json:"encryption-password"`

Files *bool `json:"files"`
Directories *bool `json:"directories"`
Files *bool `json:"files"`
Directories *bool `json:"directories"`
UploadChunkSize *int `json:"upload-chunk-size"`
}

func (cmd *pushCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
Expand Down Expand Up @@ -847,6 +848,7 @@ func (cmd *pushCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
cmd.EncryptionPassword = fs.String(drive.CLIEncryptionPassword, "", drive.DescEncryptionPassword)
cmd.Files = fs.Bool(drive.CLIOptionFiles, false, "push only files")
cmd.Directories = fs.Bool(drive.CLIOptionDirectories, false, "push only directories")
cmd.UploadChunkSize = fs.Int(drive.CLIOptionUploadChunkSize, 0, "specifies the size of each data chunk to be uploaded. Only set it if you want a custom chunk size. Otherwise the default value of googleapi.DefaultUploadChunkSize ie 8MiB will be used. However it must be at least googleapi.MinUploadChunkSize ie 256KiB. See https://godoc.org/google.golang.org/api/googleapi#pkg-constants")

return fs
}
Expand Down Expand Up @@ -1064,6 +1066,7 @@ func (pCmd *pushCmd) createPushOptions(absEntryPath string, definedFlags map[str
Destination: *cmd.Destination,
Encrypter: encryptFn,
ExponentialBackoffRetryCount: retryCount,
UploadChunkSize: *cmd.UploadChunkSize,
}

return opts, nil
Expand Down
5 changes: 5 additions & 0 deletions src/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ type Options struct {
// clickable files where applicable.
// See issue #697.
AllowURLLinkedFiles bool

// Chunksize is the size per block of data uploaded.
// If not set, the default value from googleapi.DefaultUploadChunkSize
// is used instead.
UploadChunkSize int
}

func (opts *Options) CryptoEnabled() bool {
Expand Down
2 changes: 2 additions & 0 deletions src/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ const (
CLIOptionDesktopLinks = "desktop-links"
CLIOptionKeepParent = "keep-parent"

CLIOptionUploadChunkSize = "upload-chunk-size"

CLIOptionExportsDumpToSameDirectory = "same-exports-dir"
)

Expand Down
43 changes: 23 additions & 20 deletions src/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,15 @@ func (g *Commands) PushPiped() error {
}

args := &upsertOpt{
parentId: parent.Id,
fsAbsPath: relToRootPath,
src: fauxSrc,
dest: rem,
mask: g.opts.TypeMask,
nonStatable: true,
ignoreChecksum: g.opts.IgnoreChecksum,
retryCount: g.opts.ExponentialBackoffRetryCount,
uploadChunkSize: g.opts.UploadChunkSize,
parentId: parent.Id,
fsAbsPath: relToRootPath,
src: fauxSrc,
dest: rem,
mask: g.opts.TypeMask,
nonStatable: true,
ignoreChecksum: g.opts.IgnoreChecksum,
retryCount: g.opts.ExponentialBackoffRetryCount,
}

rem, _, rErr := g.rem.upsertByComparison(os.Stdin, args)
Expand Down Expand Up @@ -372,14 +373,15 @@ func (g *Commands) remoteMod(change *Change) (err error) {
}

args := &upsertOpt{
parentId: parent.Id,
fsAbsPath: absPath,
src: change.Src,
dest: change.Dest,
mask: g.opts.TypeMask,
ignoreChecksum: g.opts.IgnoreChecksum,
debug: g.opts.Verbose && g.opts.canPreview(),
retryCount: g.opts.ExponentialBackoffRetryCount,
uploadChunkSize: g.opts.UploadChunkSize,
parentId: parent.Id,
fsAbsPath: absPath,
src: change.Src,
dest: change.Dest,
mask: g.opts.TypeMask,
ignoreChecksum: g.opts.IgnoreChecksum,
debug: g.opts.Verbose && g.opts.canPreview(),
retryCount: g.opts.ExponentialBackoffRetryCount,
}

coercedMimeKey, ok := g.coercedMimeKey()
Expand Down Expand Up @@ -520,10 +522,11 @@ func (g *Commands) remoteMkdirAll(d string) (*File, error) {
}

args := upsertOpt{
parentId: parent.Id,
src: remoteFile,
debug: g.opts.Verbose && g.opts.canPreview(),
retryCount: g.opts.ExponentialBackoffRetryCount,
uploadChunkSize: g.opts.UploadChunkSize,
parentId: parent.Id,
src: remoteFile,
debug: g.opts.Verbose && g.opts.canPreview(),
retryCount: g.opts.ExponentialBackoffRetryCount,
}

cur, curErr := g.rem.UpsertByComparison(&args)
Expand Down
33 changes: 20 additions & 13 deletions src/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

expb "github.com/odeke-em/exponential-backoff"
drive "google.golang.org/api/drive/v2"
"google.golang.org/api/googleapi"
)

const (
Expand Down Expand Up @@ -660,17 +661,18 @@ func indexContent(mask int) bool {
}

type upsertOpt struct {
debug bool
parentId string
fsAbsPath string
relToRootPath string
src *File
dest *File
mask int
ignoreChecksum bool
mimeKey string
nonStatable bool
retryCount int
debug bool
parentId string
fsAbsPath string
relToRootPath string
src *File
dest *File
mask int
ignoreChecksum bool
mimeKey string
nonStatable bool
retryCount int
uploadChunkSize int
}

func togglePropertiesInsertCall(req *drive.FilesInsertCall, mask int) *drive.FilesInsertCall {
Expand Down Expand Up @@ -756,11 +758,16 @@ func (r *Remote) upsertByComparison(body io.Reader, args *upsertOpt) (f *File, m
// Ensure that the ModifiedDate is retrieved from local
uploaded.ModifiedDate = toUTCString(args.src.ModTime)

var mediaOptions []googleapi.MediaOption
if args.uploadChunkSize > 0 {
mediaOptions = append(mediaOptions, googleapi.ChunkSize(args.uploadChunkSize))
}

if args.src.Id == "" {
req := r.service.Files.Insert(uploaded)

if !args.src.IsDir && body != nil {
req = req.Media(body)
req = req.Media(body, mediaOptions...)
mediaInserted = true
}

Expand All @@ -782,7 +789,7 @@ func (r *Remote) upsertByComparison(body io.Reader, args *upsertOpt) (f *File, m
req.SetModifiedDate(true)

if args.shouldUploadBody() {
req = req.Media(body)
req = req.Media(body, mediaOptions...)
mediaInserted = true
}

Expand Down