drsb77336 2015-03-30 17:27
浏览 51

Golang Google存储可恢复上传HTTP 401

Hey im trying to implement a resumable upload to cloud storage .

But im getting a Status:"401 Unauthorized", StatusCode:401

And im assuming that it`s something with the bearer but i can't figure out another way to send the bearer Token.

I've been able to delete files with the GetClinet method.

func GetClinet(c endpoints.Context) *http.Client {
    cli := &http.Client{
        Transport: &oauth2.Transport{
            Source: google.AppEngineTokenSource(c, storage.ScopeReadWrite),
            Base:   &urlfetch.Transport{Context: c},
        },
    }

    return cli

}

client := GetClinet(con)

url := "https://storage.googleapis.com/bucketName/file.txt"
b := r.Header.Get("Authorization") //parse the bearer from user request
r, err = http.NewRequest("POST", url, nil)
r.Header.Add("Content-Type", "text/plain")
r.Header.Add("Content-Length", "0")
r.Header.Add("x-goog-resumable", "start")
r.Header.Add("Authorization", b)

resp, err := client.Do(r)
  • 写回答

1条回答 默认 最新

  • dsc56927 2015-04-07 07:08
    关注

    I'm having similar difficulties, not sure if your situation is the same as mine. My goal is to

    1. User client uploads meta-data to my server
    2. Server authenticates and stores in DB
    3. Server asks Google Cloud Storage for resumabale upload url
    4. Server returns url to client
    5. Client uses url to upload media to GCS

    I'm getting 401 unauthorized returns too. Maybe I'm not setting the headers correctly? or the storage.SignedURL doesn't handle resumable uploads properly...?

    I've been able to upload files using a Service Account PEM file

    func setGcsFile(c context.Context, fileId string, content []byte) error {
        expiration := time.Now().Add(time.Second * 30) //expire in 30 seconds
    
        data, err = ioutil.ReadFile(serviceAccountPEMFilename)
        if err != nil {
            fmt.Printf("error: %v", err)
            panic(err)
        }
    
        log.Debugf(c, "Getting upload url")
        opts := &storage.SignedURLOptions{
            GoogleAccessID: googleAccessID,
            PrivateKey:     data,
            Method:         "PUT",
            Expires:        expiration,
        }
    
        putURL, err := storage.SignedURL(bucket, fileId, opts)
        if err != nil {
            log.Debugf(c, "%v", err)
            return err
        }
    
        log.Debugf(c, "PUT URL : %v
    ", putURL)
    
        client := urlfetch.Client(c)
        req, err := http.NewRequest("PUT", putURL, bytes.NewBuffer(content))
        res, err := client.Do(req)
        if err != nil {
            log.Debugf(c, "%v", err)
            return err
        }
        res.Body.Close()
        log.Debugf(c, "Response Code: %s
    ", res.Status)
        return nil
    }
    

    I'm thinking about pulling and modifying the storage.SignedURL function next.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看