From 593305df637afa80ad74f99987f65b49e7dcc810 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Mon, 1 Apr 2024 02:14:28 -0600 Subject: [PATCH] Fixed S3 --- ...alable-realtime-apps-with-fly-and-remix.md | 2 +- go.mod | 2 + go.sum | 6 +++ lib/s3.go | 44 +++++++++++++++++-- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/content/scalable-realtime-apps-with-fly-and-remix.md b/content/scalable-realtime-apps-with-fly-and-remix.md index d43e0f1..0ed855e 100644 --- a/content/scalable-realtime-apps-with-fly-and-remix.md +++ b/content/scalable-realtime-apps-with-fly-and-remix.md @@ -17,7 +17,7 @@ Real-time applications require a pub/sub or publish and subscribe model. A clien For this stack, I chose Remix for its close adherence to web standards and easy support for server-sent events. These web socket connections work one way: from server to client. Next, we must synchronize all Server Sent events across different requests to a single node. For this, Node.js has its own Event Emitter API, which we can use. Now, we can use something like Redis and its Pub/Sub commands for multi-node setups to broadcast across nodes. This is what it would look like: -![Diagram](https://atridad.s3.amazonaws.com/articles/scalability.png) +![Diagram](https://link.storjshare.io/s/jwmhimh32pura4pyr5h5luou6qla/atridad%2Farticles/scalability.png?wrap=0) # How does it work? diff --git a/go.mod b/go.mod index 325b2cd..fe29e77 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/go-cmp v0.6.0 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect @@ -29,6 +30,7 @@ require ( ) require ( + github.com/aws/aws-sdk-go v1.51.11 github.com/clerkinc/clerk-sdk-go v1.49.0 github.com/disintegration/imaging v1.6.2 github.com/fatih/color v1.16.0 diff --git a/go.sum b/go.sum index 316f2cb..7fae31e 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/alecthomas/chroma/v2 v2.13.0/go.mod h1:BUGjjsD+ndS6eX37YgTchSEG+Jg9Jv github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/aws/aws-sdk-go v1.51.11 h1:El5VypsMIz7sFwAAj/j06JX9UGs4KAbAIEaZ57bNY4s= +github.com/aws/aws-sdk-go v1.51.11/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/brianvoe/gofakeit/v6 v6.19.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8= github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= @@ -49,6 +51,9 @@ github.com/gorilla/feeds v1.1.2 h1:pxzZ5PD3RJdhFH2FsJJ4x6PqMqbgFk1+Vez4XWBW8Iw= github.com/gorilla/feeds v1.1.2/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -173,6 +178,7 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lib/s3.go b/lib/s3.go index 8c33d13..80f8869 100644 --- a/lib/s3.go +++ b/lib/s3.go @@ -3,12 +3,50 @@ package lib import ( "fmt" "os" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/s3" ) func GeneratePublicURL(key string) string { bucket := os.Getenv("BUCKET_NAME") - endpoint := os.Getenv("AWS_ENDPOINT_URL_S3") + if bucket == "" { + fmt.Println("No S3 bucket specified, skipping upload.") + return "" + } - url := fmt.Sprintf("%s/%s/%s", endpoint, bucket, key) - return url + endpoint := os.Getenv("AWS_ENDPOINT_URL_S3") + accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID") + secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY") + region := os.Getenv("AWS_REGION") + + sess, err := session.NewSession(&aws.Config{ + Region: ®ion, + Credentials: credentials.NewStaticCredentials( + accessKeyID, + secretAccessKey, + "", + ), + Endpoint: aws.String(endpoint), + }) + if err != nil { + return "" + } + + svc := s3.New(sess) + + req, _ := svc.GetObjectRequest(&s3.GetObjectInput{ + Bucket: &bucket, + Key: &key, + }) + urlStr, err := req.Presign(15 * time.Minute) + + if err != nil { + return "" + } + + return urlStr }