Skip to content

Instantly share code, notes, and snippets.

@paddycarver
Created April 27, 2012 00:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddycarver/2504488 to your computer and use it in GitHub Desktop.
Save paddycarver/2504488 to your computer and use it in GitHub Desktop.
Upload a file to IronWorker from Go
package main
import (
"fmt"
"net/http"
"mime/multipart"
"bytes"
"os"
"io"
"io/ioutil"
"encoding/json"
)
type ReqData struct {
Name string `json:"name"`
FileName string `json:"file_name"`
Runtime string `json:"runtime"`
}
func main() {
project := "4f8e3....."
token := "AhEg....."
target := fmt.Sprintf("http://worker-aws-us-east-1.iron.io/2/projects/%s/codes?oauth=%s", project, token)
json_data := &ReqData {
Name: "GoWorker",
FileName: "__runner__.sh",
Runtime: "sh",
}
json_bytes, err := json.Marshal(json_data)
if err != nil {
panic(err.Error())
}
json_str := string(json_bytes)
body_buf := bytes.NewBufferString("")
body_writer := multipart.NewWriter(body_buf)
filename := "worker.zip"
file_writer, err := body_writer.CreateFormFile("file", filename)
if err != nil {
panic(err.Error())
}
fh, err := os.Open(filename)
if err != nil {
panic(err.Error())
}
io.Copy(file_writer, fh)
body_writer.WriteField("data", json_str)
content_type := body_writer.FormDataContentType()
body_writer.Close()
resp, err := http.Post(target, content_type, body_buf)
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
fmt.Println(resp.Status)
fmt.Println(string(resp_body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment