diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2d0acac --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,21 @@ +on: + push: + tags: + - 'v*' +name: GoReleaser +jobs: + release: + name: Release + runs-on: ubuntu-latest + #needs: [ test ] + steps: + - name: Check out code + uses: actions/checkout@master + - name: goreleaser + uses: docker://goreleaser/goreleaser + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CGO_ENABLED: 0 + with: + args: release + if: success() diff --git a/.gitignore b/.gitignore index f1c181e..fc41f43 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out + +s3proxy diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..3319744 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,28 @@ +# This is an example goreleaser.yaml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +before: + hooks: + # you may remove this if you don't use vgo + - go mod tidy + # you may remove this if you don't need go generate + # - go generate ./... +builds: +- env: + - CGO_ENABLED=0 +# archives: +# - replacements: +# darwin: Darwin +# linux: Linux +# windows: Windows +# 386: i386 +# amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..230cb95 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/LOKE/s3proxy + +go 1.13 + +require ( + github.com/aws/aws-sdk-go v1.23.21 + github.com/stretchr/testify v1.4.0 // indirect + golang.org/x/net v0.0.0-20190912160710-24e19bdeb0f2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5a3bf68 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/aws/aws-sdk-go v1.23.21 h1:eVJT2C99cAjZlBY8+CJovf6AwrSANzAcYNuxdCB+SPk= +github.com/aws/aws-sdk-go v1.23.21/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190912160710-24e19bdeb0f2 h1:4dVFTC832rPn4pomLSz1vA+are2+dU19w1H8OngV7nc= +golang.org/x/net v0.0.0-20190912160710-24e19bdeb0f2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..521e77c --- /dev/null +++ b/main.go @@ -0,0 +1,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) + })) +}