-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (48 loc) · 1.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"flag"
"io"
"log"
"net/http"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
addr := flag.String("address", ":7000", "the address to bind to")
region := flag.String("region", "", "aws region")
bucket := flag.String("bucket", "", "aws bucket")
flag.Parse()
sess := session.Must(session.NewSession(&aws.Config{Region: region}))
s3Client := s3.New(sess, aws.NewConfig().WithRegion(*region))
http.ListenAndServe(*addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "HEAD" {
http.Error(w, "Resource only supports GET and HEAD", http.StatusMethodNotAllowed)
return
}
key := r.URL.Path
if strings.HasSuffix(key, "/") {
key += "index.html"
}
result, err := s3Client.GetObject(&s3.GetObjectInput{
Bucket: bucket,
Key: &key,
})
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchKey:
http.NotFound(w, r)
return
}
log.Printf("unknown aws error %v", err)
http.Error(w, "unknown aws error", http.StatusInternalServerError)
}
defer result.Body.Close()
if result.ContentType != nil {
w.Header().Set("Content-Type", *result.ContentType)
}
_, err = io.Copy(w, result.Body)
}))
}