aboutsummaryrefslogtreecommitdiff
path: root/go/operation/upload_content.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2013-02-10 03:49:51 -0800
committerChris Lu <chris.lu@gmail.com>2013-02-10 03:49:51 -0800
commit5071f528f649f3f99336c7d491ceef4859e34744 (patch)
tree0c4bc8a286597cd79e22b1ce02cd9cd3b1c44602 /go/operation/upload_content.go
parent55f2627fcf965c3765ad9b63878e9a22a59f4b55 (diff)
downloadseaweedfs-5071f528f649f3f99336c7d491ceef4859e34744.tar.xz
seaweedfs-5071f528f649f3f99336c7d491ceef4859e34744.zip
testing compilation with remove package
Diffstat (limited to 'go/operation/upload_content.go')
-rw-r--r--go/operation/upload_content.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/go/operation/upload_content.go b/go/operation/upload_content.go
new file mode 100644
index 000000000..0bdb697da
--- /dev/null
+++ b/go/operation/upload_content.go
@@ -0,0 +1,47 @@
+package operation
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ _ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "mime/multipart"
+ "net/http"
+)
+
+type UploadResult struct {
+ Size int
+ Error string
+}
+
+func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) {
+ body_buf := bytes.NewBufferString("")
+ body_writer := multipart.NewWriter(body_buf)
+ file_writer, err := body_writer.CreateFormFile("file", filename)
+ io.Copy(file_writer, reader)
+ content_type := body_writer.FormDataContentType()
+ body_writer.Close()
+ resp, err := http.Post(uploadUrl, content_type, body_buf)
+ if err != nil {
+ log.Println("failing to upload to", uploadUrl)
+ return nil, err
+ }
+ defer resp.Body.Close()
+ resp_body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ var ret UploadResult
+ err = json.Unmarshal(resp_body, &ret)
+ if err != nil {
+ log.Println("failing to read upload resonse", uploadUrl, resp_body)
+ return nil, err
+ }
+ if ret.Error != "" {
+ return nil, errors.New(ret.Error)
+ }
+ return &ret, nil
+}