当前位置: > > > > 使用 Golang 从 Azure Blob 存储下载文件时,获取“来自服务器的curl Empty答复”,但文件是在后台下载的
来源:stackoverflow
2024-04-21 10:09:30
0浏览
收藏
学习Golang要努力,但是不要急!今天的这篇文章《使用 Golang 从 Azure Blob 存储下载文件时,获取“来自服务器的curl Empty答复”,但文件是在后台下载的》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
问题内容
我正在尝试使用 http 请求从 azure blob 存储下载文件。我可以下载该文件,但在终端上,curl 返回“来自服务器的空回复”。我尝试增加超时时间,但没有解决问题。我提到了与curl的回复相关的其他问题,但没有帮助。对于小文件,此代码可以完美运行,但对于 75 mb 的大文件,它无法运行。
containerURL := azblob.NewContainerURL(*URL, pipeline) blobURL := containerURL.NewBlockBlobURL(splitArray[1]) ctx := context.Background() downloadResponse, err := blobURL.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false) if err != nil { . . . } bodyStream := downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20}) // read the body into a buffer downloadedData := bytes.Buffer{} _, err = downloadedData.ReadFrom(bodyStream) file, err := os.OpenFile( "/tmp/"+fileName, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0777, ) file.Write(downloadedData.Bytes()) file.Close() filePath := "/tmp/" + fileName file, err = os.Open(filePath) return middleware.ResponderFunc(func(w http.ResponseWriter, r runtime.Producer) { fn := filepath.Base(filePath) w.Header().Set(CONTENTTYPE, "application/octet-stream") w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", fn)) io.Copy(w, file) err := defer os.Remove(filePath) file.Close() })
我正在考虑使用 goroutine 来实现上述逻辑。是否需要使用 goroutine?
任何建设性的反馈都会有所帮助。
解决方案
分析来自wireshark的数据包后发现,由于我使用go-swagger时超时,它与我这边断开了连接,我在configure.go中增加了超时。 goswagger 提供了内置函数来处理这些场景,例如 tls 、超时。以下是参考代码。
// As soon as server is initialized but not run yet, this function will be called. // If you need to modify a config, store server instance to stop it individually later, this is the place. // This function can be called multiple times, depending on the number of serving schemes. // scheme value will be set accordingly: "http", "https" or "unix" func configureServer(s *http.Server, scheme, addr string) { s.WriteTimeout(time.Minute * 5) }
今天关于《使用 Golang 从 Azure Blob 存储下载文件时,获取“来自服务器的curl Empty答复”,但文件是在后台下载的》的内容介绍就到此结束,如果有什么疑问或者建议,可以在公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!