douchuang8359 2018-04-09 07:38
浏览 24
已采纳

如何验证来自appEngine flexible的可恢复上传内容,类似于golang中的SignedURL

I got this code to get a signedURL:

import (
    "net/http"
    "time"
    "cloud.google.com/go/storage"
    "google.golang.org/appengine"
)

//Don´t worry about the errors, I´m handling them
func CreatSignedURL(r *http.Request) (string, error) {
    ctx := appengine.NewContext(r)
    acc, _ := appengine.ServiceAccount(ctx)
    filename := "fileName"
    bucket := "bucketName"
    expires := time.Now().Add(time.Hour * 2)
    url, _ := storage.SignedURL(bucket, filename, &storage.SignedURLOptions{
        GoogleAccessID: acc,
        SignBytes: func(b []byte) ([]byte, error) {
            _, signedBytes, err2 := appengine.SignBytes(ctx, b)
            return signedBytes, err2
        },
        Method:  "PUT",
        Expires: expires,
    })
    return url, nil
}

But, I want to authenticate similar as the code above with resumable upload

import (
    "fmt"
    "net/http"
)

func ResumeUploadURL(nameBucket, nombreObjeto string) string {
    url := "https://www.googleapis.com/upload/storage/v1/b/" + nameBucket + "/o?uploadType=resumable&name=" + nombreObjeto
    client := &http.Client{}
    r, _ := http.NewRequest("POST", url, nil)              // URL-encoded payload
    r.Header.Add("Authorization", "auth_token=??????????") // What should I use instead?
    r.Header.Add("Content-Length", "0")

    resp, _ := client.Do(r)
    .....
}

But I need auth_token,where auth_token I need to get it like it´s described here https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing But I would wanna avoid that becasue it seems pretty silly to me to authenticate in that way if I can easy authenticate with signedURL and with resume upload can´t

  • 写回答

1条回答 默认 最新

  • douxian5963 2018-04-12 04:12
    关注

    Well, I wanted to avoid to use auth2 because there wasn´t a nice documentation, and after reading a lot documentation and tried many fails examples, I finally go it to work, these are the steps to make a the token Bearer for Resume upload with json api

    1. Download JSON credential from console.cloud.google.com/apis/credentials

      1.1)Select "Creat Credentials" 1.2)Select "Service account key" 1.3)Select your account 1.4) Select JSON->creat 1.4)It will download a JSON with the information you need

    2. Creat a oauth2/jwt tokenSource

      import ( "context" "net/http" "golang.org/x/oauth2/jwt" ) var config = &jwt.Config{ Email: "someEmail@something.com", PrivateKey: []byte("-----BEGIN PRIVATE KEY----- bablablablablablabalabal bablablablablablabalabalas= -----END PRIVATE KEY----- "), PrivateKeyID: "somethingVeryPrivate", Scopes: []string{"https://www.googleapis.com/auth/devstorage.read_write"}, //you can change/add scopes if you want https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing TokenURL: "https://accounts.google.com/o/oauth2/token", } token, err := config.TokenSource(context.Background()).Token()//handle error

    3)Now you have the id to use, it is in method token.AccessToken

    All the procces to get the url, I put it here: https://github.com/johnbalvin/google-cloud-go/blob/master/storage/resumableUpload.go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符