Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create an empty file #57

Closed
AndreKR opened this issue Sep 12, 2018 · 7 comments
Closed

Unable to create an empty file #57

AndreKR opened this issue Sep 12, 2018 · 7 comments
Labels

Comments

@AndreKR
Copy link

AndreKR commented Sep 12, 2018

Trying to create an empty file:

obj := bucket.Object("foo")
wc := obj.NewWriter(context.Background())
fmt.Println(wc.Write([]byte{}))
fmt.Println(wc.Close())

produces the error: b2_upload_file: 400: Missing header: Content-Length

@kurin kurin added the bug label Sep 12, 2018
@kurin
Copy link
Owner

kurin commented Sep 12, 2018

I don't think this is me:

2018/09/12 10:53:43 >> b2_upload_file uri: https://pod-000-1039-13.backblaze.com/b2api/v1/b2_upload_file/72bdd680e4a7c5e76353081d/c001_v0001039_t0054 {X-Bz-Content-Sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709;X-Blazer-Request-Id: 14;X-Bz-Test-Mode: fail_some_uploads,expire_some_account_authorization_tokens;X-Bz-File-Name: empty;Content-Type: application/octet-stream;Content-Length: 0;User-Agent: integration-test b2-test blazer/0.5.1} (no args)
2018/09/12 10:53:43 << b2_upload_file (14) 400  {Cache-Control: max-age=0, no-cache, no-store; Content-Type: application/json;charset=utf-8; Content-Length: 91; Date: Wed, 12 Sep 2018 17:53:43 GMT} ({
  "code": "bad_request",
  "message": "Missing header: Content-Length",
  "status": 400
})
2018/09/12 10:53:43 error writing empty: b2_upload_file: 400: Missing header: Content-Length

@kurin
Copy link
Owner

kurin commented Sep 12, 2018

Although I think if you create a writer and immediately Close it, you should get an empty object as well, and right now you don't, with no error.

@kurin
Copy link
Owner

kurin commented Sep 12, 2018

I couldn't open a bug on the backblaze site because the recaptcha form was broken, but I'll try again later.

@AndreKR
Copy link
Author

AndreKR commented Sep 12, 2018

It's not just B2 either, this works for uploading an empty file:

// Login
req, _ := http.NewRequest("GET", "https://api.backblazeb2.com/b2api/v1/b2_authorize_account", nil)
req.SetBasicAuth(username, password)
resp, err := http.DefaultClient.Do(req)
fmt.Println(err)
d := json.NewDecoder(resp.Body)
var auth struct {
	ApiUrl             string
	AuthorizationToken string
}
err = d.Decode(&auth)
resp.Body.Close()
fmt.Println(err)
fmt.Println(auth)

// Get upload URL
req, _ = http.NewRequest("POST", auth.ApiUrl+"/b2api/v1/b2_get_upload_url", bytes.NewReader([]byte(`{"bucketId": "`+bucket+`"}`)))
req.Header.Set("Authorization", auth.AuthorizationToken)
resp, err = http.DefaultClient.Do(req)
fmt.Println(err)
d = json.NewDecoder(resp.Body)
var upload struct {
	AuthorizationToken string
	UploadUrl          string
}
err = d.Decode(&upload)
resp.Body.Close()
fmt.Println(err)
fmt.Println(upload)

// Upload
req, _ = http.NewRequest("POST", upload.UploadUrl, bytes.NewReader([]byte(""))) // empty body
req.Header.Set("Authorization", upload.AuthorizationToken)
req.Header.Set("X-Bz-File-Name", "empty")
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("Content-Length", "0") // it works without this, probably Go sets it
req.Header.Set("X-Bz-Content-Sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")
resp, err = http.DefaultClient.Do(req)
fmt.Println(err)
b, err := ioutil.ReadAll(resp.Body)
fmt.Println(err)
fmt.Println(string(b))

@kurin
Copy link
Owner

kurin commented Sep 12, 2018

It looks like the http lib is setting the transfer encoding to "chunked" and then sending a non-empty body for reasons I don't yet understand.

@kurin
Copy link
Owner

kurin commented Sep 12, 2018

POST /b2api/v1/b2_upload_file/22ed46e0b407c5f76353081d/c001_v0001105_t0039 HTTP/1.1
Host: pod-000-1105-00.backblaze.com
User-Agent: integration-test b2-test blazer/0.5.1
Transfer-Encoding: chunked
Authorization: boop
Content-Type: application/octet-stream
X-Blazer-Method: b2_upload_file
X-Blazer-Request-Id: 7
X-Bz-Content-Sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
X-Bz-File-Name: empty
X-Bz-Test-Mode: fail_some_uploads
X-Bz-Test-Mode: expire_some_account_authorization_tokens
Accept-Encoding: gzip

0


I think I want to explicitly set the encoding to "identity" but uh, that's not working.

@kurin
Copy link
Owner

kurin commented Sep 12, 2018

// outgoingLength reports the Content-Length of this outgoing (Client) request.
// It maps 0 into -1 (unknown) when the Body is non-nil.
func (r *Request) outgoingLength() int64 {

Cool. Coolcoolcool.

kurin added a commit that referenced this issue Sep 12, 2018
@kurin kurin closed this as completed in 524dfc1 Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants