下载视频时,使用“go-ipfs-api”出现致命错误:运行时:内存耗尽
时间:2024-03-04
浏览:199
作者:佚名
你在学习Golang相关的知识吗?本文《下载视频时,使用“go-ipfs-api”出现致命错误:运行时:内存耗尽》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!
问题内容
我使用go-ipfs-api从ipfs下载一个大文件,web访问下载。
我收到错误致命错误:
运行时:内存不足。
如何修改我的代码?
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-ipfs-api”出现致命错误:运行时:内存耗尽》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
