Skip to content

Commit

Permalink
Implement PreSigned Url
Browse files Browse the repository at this point in the history
We generated a Presigned Url for PUT and GET

PUT Presigned url will be used to make a put request to upload an image to our S3 bucket

GET presinged url will be used to load an image

Both links expires after 5 mins

FYI: I did some code refactoring in for our multiplexer. Route matching with the julienschmidt multiplexer was so bad!
gin-gonic/gin#1301
  • Loading branch information
okpalaChidiebere committed May 18, 2021
1 parent 1f6171b commit 9ceebe4
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 17 deletions.
78 changes: 78 additions & 0 deletions aws/filestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package aws

import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/udacity/udagram-restapi-golang/config"
)

var (
c = config.NewConfig()
)

type S3client struct {
client *s3.S3
}

// Creates a S3 client
func createS3Client() *s3.S3 {

/*
Initialize a session that the SDK will use to load
credentials from the shared credentials file ~/.aws/credentials
and region from the shared configuration file ~/.aws/config.
*/
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: c.Aws_profile,
}))

svc := s3.New(sess, aws.NewConfig().WithRegion(c.Aws_region))
return svc
}

// NewDynamoDbRepo creates a new DynamoDb Repository
func NewS3client() *S3client {
s3c := createS3Client()

return &S3client{s3c}
}

/* GetGetSignedUrl generates an aws signed url to retreive an item
* @Params
* key: string - the filename to be put into the s3 bucket
* @Returns:
* a url as a string and error
*/
func (s *S3client) GetGetSignedUrl(key string) (string, error) {

req, _ := s.client.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(c.Aws_media_bucket),
Key: aws.String(key),
})

urlStr, err := req.Presign(5 * time.Minute) //we want the expire time of the url to be about 5 minutes

return urlStr, err

}

/* GetPutSignedUrl generates an aws signed url to put an item
* @Params
* key: string - the filename to be retreived from s3 bucket
* @Returns:
* a url as a string and error
*/
func (s *S3client) GetPutSignedUrl(key string) (string, error) {
req, _ := s.client.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(c.Aws_media_bucket),
Key: aws.String(key),
})

urlStr, err := req.Presign(5 * time.Minute) //we want the expire time of the url to be about 5 minutes

return urlStr, err
}
37 changes: 30 additions & 7 deletions controllers/v0/feed/feed.router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"log"
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/gorilla/mux"
)

func IndexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func IndexHandler(w http.ResponseWriter, r *http.Request) {
fds, err := AllFeedItems()
if err != nil {
log.Printf("Problem getting all feeds: %s", err.Error())
Expand All @@ -22,7 +22,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Write(body)
}

func CreateFeedItemHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func CreateFeedItemHandler(w http.ResponseWriter, r *http.Request) {
i, err := PostFeedItem(r)
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
Expand All @@ -34,10 +34,10 @@ func CreateFeedItemHandler(w http.ResponseWriter, r *http.Request, _ httprouter.
w.Write(body)
}

func GetFeedItemHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.ByName("id")
//validation to make sure there is an id present
if id == "" {
func GetFeedItemHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, ok := vars["id"]
if !ok {
http.Error(w, http.StatusText(400), http.StatusBadRequest)
return
}
Expand All @@ -52,3 +52,26 @@ func GetFeedItemHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
w.Header().Set("Content-Type", "application/json")
w.Write(body) //we return the record to the client in a sensible payload
}

func GetGetSignedUrlHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
fn, ok := vars["fileName"]
if !ok {
http.Error(w, http.StatusText(400), http.StatusBadRequest)
return
}

url, err := GetGetSignedUrl(fn)
if err != nil {
log.Printf("Problem getting signed url for %s: %s", fn, err.Error())

http.Error(w, http.StatusText(500), http.StatusInternalServerError)
return
}

body, _ := json.Marshal(map[string]interface{}{
"url": url,
})
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(body) //we return the record to the client in a sensible payload
}
23 changes: 21 additions & 2 deletions controllers/v0/feed/feedItem.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ type FeedItem struct {

type CreateFeedItemRequest struct {
Caption string `json:"caption"`
Url string `json:"url"`
Url string `json:"url"` //eg: imageName.jpeg
}

var (
s3s = aws.NewS3client()
)

func AllFeedItems() ([]FeedItem, error) {
rows, err := aws.DB.Query("SELECT id, caption, url, created_at, updated_at FROM feeditem;")
if err != nil {
Expand All @@ -43,6 +47,15 @@ func AllFeedItems() ([]FeedItem, error) {
if err = rows.Err(); err != nil {
return nil, err
}

for i, f := range fis {
u, err := s3s.GetGetSignedUrl(f.Url)
if err != nil {
log.Printf("Error getting getSignedUrl for key %s: %s", f.Url, err.Error())
}
fis[i].Url = u
}

return fis, nil
}

Expand All @@ -57,7 +70,7 @@ func PostFeedItem(req *http.Request) (FeedItem, error) {
}

item.Caption = ni.Caption
item.Url = "https://s3-us-west-1.amazonaws.com/udacity-content/images/icon-eror.svg" //mock value for now
item.Url = ni.Url
item.CreatedAt = time.Now().Format(time.RFC3339)
item.UpdatedAt = time.Now().Format(time.RFC3339)

Expand Down Expand Up @@ -86,3 +99,9 @@ func GetFeedItem(id string) (FeedItem, error) {

return fi, nil
}

func GetGetSignedUrl(key string) (string, error) {
//c := aws.NewS3client()

return s3s.GetPutSignedUrl(key)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/udacity/udagram-restapi-golang
go 1.16

require (
github.com/aws/aws-sdk-go v1.38.40 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/julienschmidt/httprouter v1.3.0
github.com/lib/pq v1.10.1
)
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
github.com/aws/aws-sdk-go v1.38.40 h1:VVqBFV24tGgXR11tFXPjmR+0ItbnUepbuQjdmhgu3U0=
github.com/aws/aws-sdk-go v1.38.40/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo=
github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
36 changes: 28 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"os"

"github.com/julienschmidt/httprouter"
"github.com/gorilla/mux"
fh "github.com/udacity/udagram-restapi-golang/controllers/v0/feed"
)

Expand All @@ -15,15 +15,35 @@ func main() {
port = "8080"
}

mux := httprouter.New()
r := mux.NewRouter()

mux.GET("/", index)
mux.GET("/api/v0/feed", fh.IndexHandler)
mux.POST("/api/v0/feed", fh.CreateFeedItemHandler)
mux.GET("/api/v0/feed/:id", fh.GetFeedItemHandler)
http.ListenAndServe(":"+port, mux)
/*
Create a "Subrouter" dedicated to /api which will use the PathPrefix
more on nesting routes with gorrila mux here https://stackoverflow.com/questions/25107763/nested-gorilla-mux-router-does-not-work
https://binx.io/blog/2018/11/27/go-gorilla/
*/
apiRouter := mux.NewRouter().PathPrefix("/api").Subrouter().StrictSlash(true)

// This step is where we connect our "root" router and our "Subrouter" together.
r.PathPrefix("/api").Handler(apiRouter)

// Define "root" routes using r
r.Methods("GET").Path("/").HandlerFunc(index)

// Define "Subrouter" routes using apiRouter, prefix is /api
apiRouter.Methods("GET").Path("/").HandlerFunc(api)
apiRouter.Methods("GET").Path("/v0/feed").HandlerFunc(fh.IndexHandler)
apiRouter.Methods("POST").Path("/v0/feed").HandlerFunc(fh.CreateFeedItemHandler)
apiRouter.Methods("GET").Path("/v0/feed/{id}").HandlerFunc(fh.GetFeedItemHandler)
apiRouter.Methods("GET").Path("/v0/feed/signed-url/{fileName}").HandlerFunc(fh.GetGetSignedUrlHandler)

http.ListenAndServe(":"+port, r)
}

func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "/api/v0/")
}

func api(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "v0")
}

0 comments on commit 9ceebe4

Please sign in to comment.