go - 我收到一个错误致命错误:运行时:使用“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() 。
推荐阅读
- vba - VBA,使用标题名称将值从一张表复制到另一张表
- go - 所有测试的 Golang 全局设置(在相同和其他子模块中)。
- sql-server - SISS - 检查 Null DerivedColumn
- sql - 使用嵌套的 IFNULL 沿互斥路径检查表?
- javascript - 使用 node.js 更改目录
- r - 将数据从一列移动到另一列而不覆盖所有数据 R
- mysql - Mysql查询以按用户搜索状态为VALID的潜在客户数
- swift - takeValueFrom 立即更新
- azure-devops - 停止/重新启动 VSTS 代理交互模式的活动会话
- xamarin - Xamarin Forms 3.1 UWP DisplayActionSheet - 参数不正确。放置目标需要在可视化树中