net/http: HTTP/1.x 传输连接断开:http: ContentLength=2514 正文长度为 0
net/http: HTTP/1.x transport connection broken: http: ContentLength=2514 with Body length 0
我正在尝试将 nodejs 应用程序转换为 Go。在这里,我试图将文件上传到 B2。但是我得到了 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
。这是我的代码:
// 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'];
您在这里使用了文件:
if _, err := io.Copy(hash, file); err != nil {
log.Fatal(err)
return "", err
}
使用func (*File) Seek
或加载文件到内存。
我正在尝试将 nodejs 应用程序转换为 Go。在这里,我试图将文件上传到 B2。但是我得到了 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
。这是我的代码:
// 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'];
您在这里使用了文件:
if _, err := io.Copy(hash, file); err != nil {
log.Fatal(err)
return "", err
}
使用func (*File) Seek
或加载文件到内存。