douyinzha5820 2019-01-04 16:34
浏览 67

用于Google云存储的Golang SDK:分段下载

Using GoLang SDK for google cloud storage.

Cannot find how to download files in chunks.

  • 写回答

1条回答 默认 最新

  • douyuliu9527 2019-01-05 00:44
    关注

    The Google Cloud documentation says to download an object from Cloud Storage, you should use the following:

    rc, err := client.Bucket(bucket).Object(object).NewReader(ctx)
    if err != nil {
            return nil, err
    }
    defer rc.Close()
    
    data, err := ioutil.ReadAll(rc)
    if err != nil {
            return nil, err
    }
    return data, nil
    

    Source: https://cloud.google.com/storage/docs/downloading-objects#storage-download-object-code_sample

    Given their SDK returns an io.Reader, you don't need to worry about the underlying method being used to be able to reference the download in chunks (although, quickly looking through their source, it just implements http.NewRequest, which does what you want, using the same logic).

    The reason it doesn't seem to "chunked" from their example is because of the usage of ioutil.ReadAll, which although great for simple use cases, extracts all of the Readers data into memory (meaning it also has to wait for the data to become available).

    For a better understanding of how to deal with a Reader in steps, I recommend taking a look at https://tour.golang.org/methods/21 for a tour of io.Reader and how you can use it more efficiently.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改