Skip to content

Instantly share code, notes, and snippets.

Created July 23, 2019 06:31
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Resumable Uploading Files to Google Drive using Golang

Resumable Uploading Files to Google Drive using Golang

This is a sample script for the resumable upload of Files to Google Drive using Golang. This script uses the library of google-api-go-client. About the installation of google-api-go-client, please check the Quickstart for golang at the official site.

Sample script:

package main

import (

	drive ""


// ServiceAccount : Use Service account
func ServiceAccount(credentialFile string) *http.Client {
	b, err := ioutil.ReadFile(credentialFile)
	if err != nil {
	var c = struct {
		Email      string `json:"client_email"`
		PrivateKey string `json:"private_key"`
	json.Unmarshal(b, &c)
	config := &jwt.Config{
		Email:      c.Email,
		PrivateKey: []byte(c.PrivateKey),
		Scopes: []string{
		TokenURL: google.JWTTokenURL,
	client := config.Client(oauth2.NoContext)
	return client

func main() {
	filename := "sample.txt"     // Filename
	baseMimeType := "text/plain" // MimeType
	client := ServiceAccount("credential.json") // Please set the json file of Service account.

	srv, err := drive.New(client)
	if err != nil {
	file, err := os.Open(filename)
	if err != nil {
	fileInf, err := file.Stat()
	if err != nil {
	defer file.Close()
	f := &drive.File{Name: filename}
	res, err := srv.Files.
		ResumableMedia(context.Background(), file, fileInf.Size(), baseMimeType).
		ProgressUpdater(func(now, size int64) { fmt.Printf("%d, %d\r", now, size) }).
	if err != nil {
	fmt.Printf("%s\n", res.Id)


  • In this sample, the Service account is used. So the file is uploaded to the Service account's Drive. When client retrieved from OAuth2 is used, the file is uploaded to owner's Drive.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment