net/http:HTTP/1.x 传输连接中断:http:ContentLength=2514,正文长度为 0

发布于 2025-01-13 23:42:50 字数 1696 浏览 6 评论 0原文

我正在尝试将 Nodejs 应用程序转换为 Go。这里我尝试将文件上传到B2。但我收到 Post "https://pod-XX.backblaze.com/b2api/v2/b2_upload_file/WERTGVWGTE/cSEREf": net/http: HTTP/1.x 传输连接中断: http: ContentLength=3312主体长度为 0 。这是我的代码:


// open file
    file, err := os.Open(location)
    if err != nil {
        log.Fatal(err)
        return "", err
    }
    defer file.Close()

    // create sha1 hash of file
    hash := sha1.New()
    if _, err := io.Copy(hash, file); err != nil {
        log.Fatal(err)
        return "", err
    }
    sha1Sum := hex.EncodeToString(hash.Sum(nil))



    // http client
    client := http.Client{}
    req, _ := http.NewRequest("POST", b2.UploadUrl, file)
    contentLength, _ := file.Stat()
    
    req.ContentLength = contentLength.Size()  // without this, its throwing map[code:bad_request message:Missing header: Content-Length status:400]
    req.Header.Set("Content-Type", "application/octet-stream")
    req.Header.Set("Authorization", b2.AuthorizationToken)
    req.Header.Set("X-Bz-File-Name", name)
    req.Header.Set("X-Bz-Content-Sha1", sha1Sum)

    res , errr := client.Do(req)
    if errr != nil {
        log.Fatalln(errr)
        return "", errr
    }

仅供参考,这里是完美运行的nodejs代码:

const file = await fs.readFile(location);
    const sha1 = crypto.createHash('sha1').update(file).digest("hex");
    const config = {
        headers: {
            'Authorization': auth_token,
            'X-Bz-File-Name': final_name,
            'Content-Type': 'b2/x-auto',
            'X-Bz-Content-Sha1': sha1
        }
    };
    let response = await axios.post(upload_url, file, config);
    return response.data['fileId'];

I am trying to convert a nodejs app to Go. Here I am trying to upload a file to B2. But I am getting Post "https://pod-XX.backblaze.com/b2api/v2/b2_upload_file/WERTGVWGTE/cSEREf": net/http: HTTP/1.x transport connection broken: http: ContentLength=3312 with Body length 0 . Here is my code:


// open file
    file, err := os.Open(location)
    if err != nil {
        log.Fatal(err)
        return "", err
    }
    defer file.Close()

    // create sha1 hash of file
    hash := sha1.New()
    if _, err := io.Copy(hash, file); err != nil {
        log.Fatal(err)
        return "", err
    }
    sha1Sum := hex.EncodeToString(hash.Sum(nil))



    // http client
    client := http.Client{}
    req, _ := http.NewRequest("POST", b2.UploadUrl, file)
    contentLength, _ := file.Stat()
    
    req.ContentLength = contentLength.Size()  // without this, its throwing map[code:bad_request message:Missing header: Content-Length status:400]
    req.Header.Set("Content-Type", "application/octet-stream")
    req.Header.Set("Authorization", b2.AuthorizationToken)
    req.Header.Set("X-Bz-File-Name", name)
    req.Header.Set("X-Bz-Content-Sha1", sha1Sum)

    res , errr := client.Do(req)
    if errr != nil {
        log.Fatalln(errr)
        return "", errr
    }

Just for reference here is the nodejs code that is perfectly working:

const file = await fs.readFile(location);
    const sha1 = crypto.createHash('sha1').update(file).digest("hex");
    const config = {
        headers: {
            'Authorization': auth_token,
            'X-Bz-File-Name': final_name,
            'Content-Type': 'b2/x-auto',
            'X-Bz-Content-Sha1': sha1
        }
    };
    let response = await axios.post(upload_url, file, config);
    return response.data['fileId'];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2025-01-20 23:42:50

您已在此处使用文件:

if _, err := io.Copy(hash, file); err != nil {
        log.Fatal(err)
        return "", err
    }

使用 func (*File) Seek或将文件加载到内存。

You have consumed file here:

if _, err := io.Copy(hash, file); err != nil {
        log.Fatal(err)
        return "", err
    }

Use func (*File) Seek or load file to memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文