Help us understand the problem. What is going on with this article?

Dropbox SDK for Goを使ったファイルのアップロードやダウンロード

概要

Dropbox SDK for Goという非公式のパッケージを使ってドロップボックスにファイルのアップロードやダウンロードを行う方法を紹介します。

go version go1.12.3 darwin/amd64

パッケージのインストール

go get github.com/dropbox/dropbox-sdk-go-unofficial/dropbox

インスタンスの生成

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

config := dropbox.Config{
    Token: "DROPBOX_API_TOKEN",
}
client := files.New(config)

ファイル一覧の取得

arg := files.NewListFolderArg("path")

res, err := client.ListFolder(arg)
if err != nil {
    panic(err)
}

for _, e := res.Entries {
    f, ok := e.(*files.FileMetadata)
    if ok {
        fmt.Println(f.Name)
    }
}

ファイルのダウンロード

arg := files.NewDownloadArg("filepath")

_, err := client.Download(arg)
if err != nil {
    panic(err)
}

ファイルのアップロード

新規ファイルのアップロード

arg := files.NewCommitInfo("filepath")

// io.Reader型ならなんでも良い
file, _ := os.Create("sample.txt")

_, err := client.Upload(arg, file)
if err := nil {
    panic(err)
}

既存ファイルへのアップロード(上書き)

arg := files.NewGetMetadataArg("filepath")

res, err := client.GetMetadata(arg)
if err != nil {
    panic(err)
}

var rev string
m, ok := res.(*files.FileMetadata)
if ok {
    rev = m.Rev
}

arg = files.NewCommitInfo("filepath")
args.Mode = &files.WriteMode{Tagged: dropbox.Tagged{"update"}, Update: rev}

file, _ := os.Create("sample.txt")

_, err := client.Upload(arg, file)
if err != nil {
    panic(err)
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away