Created
April 27, 2012 00:30
-
-
Save paddycarver/2504488 to your computer and use it in GitHub Desktop.
Upload a file to IronWorker from Go
This file contains hidden or 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
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