LoginSignup
4
4

More than 5 years have passed since last update.

goでdropboxにファイルアップロード

Posted at

概要

dbのバックアップとユーザが上げた画像をzipにして
dropboxにあげるスクリプトをgoで書いた。
そのアップロード部分で若干ハマったのでsampleを残しておく

github

sample

使用したライブラリ
github

package main

import (
    "fmt"
    "os"

    "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
    "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/files"
)

func main() {
    config := dropbox.Config{
        Token:    "<your-access-token>",
        LogLevel: dropbox.LogInfo, // if needed, set the desired logging level. Default is off
    }

    cli := files.New(config)

    req := files.NewCommitInfo("<file-path>")
    f, _ := os.Open("<file-path>")
    res, err := cli.Upload(req, f)

    if err != nil {
        fmt.Printf("\n\n*** Error %v\n\n", err)
        return
    }
    fmt.Printf("\n\n%#v\n\n", res)
}

アクセストークン

ここから アプリを作る

アプリページに Generated access token があるので生成してそれを使う

期限とか特に書いてなかったので使ってる内に切れたらもう少し深く調べてみる

ハマったこと

アップロードしたファイルが格納されるのは

  • Root(Dropbox)
    • アプリ ← 自分の環境ではこの名前になっていた
      • application-name
        • file-path

なのでDropbox上にapplication-nameのフォルダ作っても
一向に格納されないので注意が必要かと。

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