azure - 使用 Golang 从 Azure Blob 存储下载文件时得到 "curl Empty reply from server",但文件是在后台下载的

标签 azure go swagger swagger-2.0 goroutine

我正在尝试使用 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 吗?

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

最佳答案

在分析了来自 wireshark 的数据包后,我知道由于我正在使用 go-swagger 超时,它正在与我的身边断开连接,我在 中增加了超时时间。配置.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)
}

关于azure - 使用 Golang 从 Azure Blob 存储下载文件时得到 "curl Empty reply from server",但文件是在后台下载的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320323/

相关文章:

node.js - 重定向到redirectURL Passport-azure-ad OIDCStrategy

optimization - 约束单目标优化

c# - Swagger 5.0 - IDocumentFilter(SwaggerDocument 到 OpenApiDocument)自定义 "Definitions"

c# - WCF REST 服务的 Azure 缓存间歇性响应时间

azure - 访问 Azure AD 图形资源管理器时出错

go - 如何在 golang 标志中记录其余的 argv?

dictionary - 使用 go lang 中的 map 实现更高阶的 fizz buzz?

swagger - 将 JWT Bearer Auth 添加到 OpenAPI3 NSwag 生成的客户端

spring-boot - Open API Spec V2.0 - 枚举类型字段的默认值

python - 在 Azure Web 应用程序中安装 psycopg2