Created
November 26, 2012 07:19
-
-
Save felixge/4147006 to your computer and use it in GitHub Desktop.
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 ( | |
"net/http" | |
"fmt" | |
"io" | |
"strconv" | |
"launchpad.net/goamz/aws" | |
"launchpad.net/goamz/s3" | |
) | |
func handler(w http.ResponseWriter, r *http.Request) { | |
var err error | |
switch r.Method { | |
case "GET": | |
err = get(w, r) | |
case "POST": | |
err = post(w, r) | |
} | |
if (err != nil) { | |
w.Write([]byte(err.Error())) | |
} | |
} | |
func get(w http.ResponseWriter, r *http.Request) error { | |
url := r.URL.Path; | |
s3Url := fmt.Sprintf("http://felixgo.s3.amazonaws.com%s", url) | |
resp, err := http.Get(s3Url) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
_, err = io.Copy(w, resp.Body) | |
return err | |
} | |
func post(w http.ResponseWriter, r *http.Request) error { | |
auth := aws.Auth{AccessKey: "secret", SecretKey: "secret"} | |
s := s3.New(auth, aws.EUWest) | |
bucket := s.Bucket("felixgo") | |
length, err := strconv.Atoi(r.Header.Get("Content-Length")) | |
err = bucket.PutReader(r.URL.Path, r.Body, int64(length), "text/plain", s3.PublicRead) | |
return err | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
err := http.ListenAndServe(":8080", nil) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment