使用 Golang 从 Azure Blob 存储下载文件时获取" curl Empty reply from server",但文件在后台下载



我正在尝试使用 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()
})

我正在考虑使用 goroutines 实现上述逻辑。甚至需要使用goroutines吗?

任何建设性的反馈都会有所帮助。

在分析了来自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)
}

最新更新