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

Goの様々な知見

自分がGoぶつかった課題の解答をまとめます

#1ファイルをGCSに上げるときにぶつかった

//リクエストのファイル取得
file, handler, err := this.Ctx.Request.FormFile("file")
//コンテキスト作成
ctx := context.Background()
//トークン使ってクライアント作成
client, err := storage.NewClient(ctx, option.WithCredentialsFile("/go/src/keipass-api/conf/xxx.json"))
//バケットと名前指定して
writer := client.Bucket("xxx").Object("xxx.png").NewWriter(ctx)
//保存
writer.Write(file) <-ファイルがバイナリじゃないって怒られる

multipart.Fileはio.Readerインターフェースを実装しているため、次のようにそのコンテンツをbytes.Bufferにコピーする。

file, header, err := ctx.Request.FormFile("file")    
defer file.Close()
if err != nil {
    return nil, err
}

buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
    return nil, err
}

#2 Google Storageの権限付与

client, err := storage.NewClient(ctx)
writer := client.Bucket("xxx").Object("xxx.png").NewWriter(ctx)
//ACLに権限情報を埋め込む
writer.ACL = []storage.ACLRule{{Entity: storage.AllUsers, Role: storage.RoleReader}}
Why do not you register as a user and use Qiita more conveniently?
  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