douquanhui5735 2015-01-08 15:59
浏览 129
已采纳

如何使用go http.Client(和应用程序引擎urlfetch)仅下载大文件的开头

I'm trying to download only the first 1kb of large files using http.Client in go, but it seems like response.Body is always fully buffered. Is there a control over how much to buffer?

If so, how can this be used with app engine urlfetch service?

The following works fine with app engine urlfetch in python, and I'm trying to port this to go:

from urllib2 import urlopen
req = Request(url)
urlopen(req).read(1024) # Read the first 1kb.
  • 写回答

2条回答 默认 最新

  • douzhang8840 2015-01-08 17:55
    关注

    Setting "Range" Request Header attribute to "bytes=0-1023" should work.

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "net/http"
    )
    
    func main() {
        req, _ := http.NewRequest("GET", "http://example.com", nil)
        req.Header.Add("Range", "bytes=0-1023")
        fmt.Println(req)
        var client http.Client
        resp, _ := client.Do(req)
        fmt.Println(resp)
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println(len(body))
    }
    

    Same thing should work for http.Client provieded in app engine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!