gpt4 book ai didi

go - 我收到错误 fatal error : runtime: out of memory while downloading video using 'go-ipfs-api'

转载 作者:数据小太阳 更新时间:2023-10-29 03:42:38 25 4
gpt4 key购买 nike

我使用go-ipfs-api从ipfs下载了一个大文件,web访问下载。

我收到一个 fatal error :

runtime: out of memory.

如何修改我的代码?

func main()  {
http.HandleFunc("/", download)

http.ListenAndServe(":8080", nil)

}

func download(w http.ResponseWriter, r *http.Request) {
client := shell.NewShell("http://127.0.0.1:5001")

fd, err := client.Cat("QmTcj7SfRf4vnLnCqnxMT7kutrzFyevjBeT5RCiN9xGAL4")
if err != nil{
fmt.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
defer fd.Close()

fileName := "demo.mp4"
// As per RFC6266 section 4.3
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+ fileName)

data, _ := ioutil.ReadAll(fd)

http.ServeContent(w, r, fileName, time.Now().Local(), bytes.NewReader(data))

w.WriteHeader(http.StatusOK)
return
}

最佳答案

您可以使用 io.Copy 代替 ReadAll,但您必须自己设置内容类型:

w.Header.Set("Content-Type",<the content type>)
io.Copy(w,fd)
return

我不熟悉你正在使用的库,你可能需要在复制后调用 fd.Close()。

关于go - 我收到错误 fatal error : runtime: out of memory while downloading video using 'go-ipfs-api' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57618925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com