Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created July 23, 2019 06:31
Show Gist options
  • Select an option

  • Save tanaikech/19655a8130bac1ba510b29c9c44bbd97 to your computer and use it in GitHub Desktop.

Select an option

Save tanaikech/19655a8130bac1ba510b29c9c44bbd97 to your computer and use it in GitHub Desktop.

Revisions

  1. tanaikech created this gist Jul 23, 2019.
    87 changes: 87 additions & 0 deletions submit.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    # 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](https://github.com/googleapis/google-api-go-client). About the installation of google-api-go-client, please check [the Quickstart](https://developers.google.com/docs/api/quickstart/go) for golang at the official site.

    ## Sample script:

    ```golang
    package main

    import (
    "context"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "os"

    drive "google.golang.org/api/drive/v3"

    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2/jwt"
    )

    // ServiceAccount : Use Service account
    func ServiceAccount(credentialFile string) *http.Client {
    b, err := ioutil.ReadFile(credentialFile)
    if err != nil {
    log.Fatal(err)
    }
    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{
    drive.DriveScope,
    },
    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 {
    log.Fatalln(err)
    }
    file, err := os.Open(filename)
    if err != nil {
    log.Fatalln(err)
    }
    fileInf, err := file.Stat()
    if err != nil {
    log.Fatalln(err)
    }
    defer file.Close()
    f := &drive.File{Name: filename}
    res, err := srv.Files.
    Create(f).
    ResumableMedia(context.Background(), file, fileInf.Size(), baseMimeType).
    ProgressUpdater(func(now, size int64) { fmt.Printf("%d, %d\r", now, size) }).
    Do()
    if err != nil {
    log.Fatalln(err)
    }
    fmt.Printf("%s\n", res.Id)
    }
    ```

    ## Note:

    - 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.

    ## References:

    - [google-api-go-client](https://github.com/googleapis/google-api-go-client/blob/master/drive/v3/drive-gen.go)
    - [package drive](https://godoc.org/google.golang.org/api/drive/v3)