LoginSignup
6
9

More than 5 years have passed since last update.

GoでGoole Driveにファイルを追加

Posted at

Google Drive REST APIのドキュメントには、GoでGoogle Driveのファイル一覧を取得するサンプルがのっています。
このサンプルを一部変えて、ファイルをアップロードするサンプルコードを作ってみました。
クライアントID、クライアントシークレットの取得などはドキュメントと同様です。

下記ファイルと同じディレクトリにアップロードしたいファイルを保存しておき、go run upload.goを実行します。
認証情報などが保存されていれば、アップロードしたいファイルのファイル名を入力すると、Google Driveのルートディレクトリにアップロードされます。

upload.go
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
    "os"
    "path/filepath"

    "golang.org/x/net/context"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "google.golang.org/api/drive/v3"
)

func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
    cacheFile, err := tokenCacheFile()
    if err != nil {
        log.Fatalf("Unable to get path to cached credentioal file. %v,", err)
    }
    tok, err := tokenFromFile(cacheFile)
    if err != nil {
        tok = getTokenFromWeb(config)
        saveToken(cacheFile, tok)
    }
    return config.Client(ctx, tok)
}

func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
    authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
    fmt.Printf("Go to the following link in your browser then type the authorization code: \n%v\n", authURL)

    var code string
    if _, err := fmt.Scan(&code); err != nil {
        log.Fatalf("Uncable to read authorization code %v", err)
    }

    tok, err := config.Exchange(oauth2.NoContext, code)
    if err != nil {
        log.Fatalf("Uncable to retrieve token from web %v", err)
    }
    return tok
}

func tokenCacheFile() (string, error) {
    tokenCacheDir := ".credentials"
    err := os.MkdirAll(tokenCacheDir, 0700)
    return filepath.Join(tokenCacheDir, url.QueryEscape("drive-go-quickstart.json")), err
}

func tokenFromFile(file string) (*oauth2.Token, error) {
    f, err := os.Open(file)
    if err != nil {
        return nil, err
    }
    t := &oauth2.Token{}
    err = json.NewDecoder(f).Decode(t)
    defer f.Close()
    return t, err
}

func saveToken(file string, token *oauth2.Token) {
    fmt.Printf("Saving credentioal file to: %s \n", file)
    f, err := os.Create(file)
    if err != nil {
        log.Fatalf("Unable to cache oauth token: %v", err)
    }
    defer f.Close()
    json.NewEncoder(f).Encode(token)
}

func main() {
    ctx := context.Background()

    b, err := ioutil.ReadFile("client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    config, err := google.ConfigFromJSON(b, drive.DriveFileScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

    srv, err := drive.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    fmt.Println("Please input file name you want to upload")
    var filename string
    if _, err := fmt.Scan(&filename); err != nil {
        log.Fatalf("Uncable to read authorization code %v", err)
    }
    uploadFile, err := os.Open(filename)
    if err != nil {
        log.Fatalf("Cannot find such file %v", err)
    }

    f := &drive.File{Name: filename, Description: "test"}

    res, err := srv.Files.Create(f).Media(uploadFile).Do()
    if err != nil {
        log.Fatalf("Upload Failed %v", err)
    }
    fmt.Println(res)
}
6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9