-
-
Save mrosset/f469a3246c514a3fbb7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upload file to google code | |
func Upload(tarball string) (err os.Error) { | |
// Create buffer | |
buf := new(bytes.Buffer) // caveat IMO dont use this for large files, \ | |
// create a tmpfile and assemble your multipart from there (not tested) | |
w := multipart.NewWriter(buf) | |
// Create a form field writer for field label | |
label, err := w.CreateFormField("label") | |
if err != nil { | |
return err | |
} | |
// Write label field | |
label.Write([]byte("label here")) | |
// Create a form field writer for field summary | |
summary, err := w.CreateFormField("summary") | |
if err != nil { | |
return err | |
} | |
// Write summary field | |
summary.Write([]byte("summary here")) | |
// Create file field | |
fw, err := w.CreateFormFile("upload", tarball) | |
if err != nil { | |
return err | |
} | |
fd, err := os.Open(tarball) | |
if err != nil { | |
return err | |
} | |
defer fd.Close() | |
// Write file field from file to upload | |
_, err = io.Copy(fw, fd) | |
if err != nil { | |
return err | |
} | |
// Important if you do not close the multipart writer you will not have a | |
// terminating boundry | |
w.Close() | |
req, err := http.NewRequest("POST", repoUrl, buf) | |
if err != nil { | |
return err | |
} | |
req.Header.Set("Content-Type", w.FormDataContentType()) | |
req.SetBasicAuth("email@email.com", "password") | |
res, err := client.Do(req) | |
if err != nil { | |
return err | |
} | |
io.Copy(os.Stderr, res.Body) // Replace this with Status.Code check | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment