Skip to content

Commit 5304622

Browse files
committed
Initial commit
1 parent 64caaf4 commit 5304622

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.env

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# AWS-S3-upload-presigned-url
22
Simple Demonstration code for upload file into s3 bucket and how to generate pre-signed URL of the uploaded file.
3+
4+
5+
# **Simple S3 upload and generate presigned url**
6+
7+
Clone the git repo - `git clone git@github.com:golang-mitrah/gin-RestAPI-postgres-orm.git` - or [download it](https://github.com/golang-mitrah/gin-RestAPI-postgres-orm/zipball/master).
8+
9+
## **Contributors**
10+
11+
Gin GoORM REST api is authored by **[GoLang Mitrah](https://www.MitrahSoft.com/)** and everyone is welcome to contribute.
12+
13+
## **Problems**
14+
15+
If you experience any problems with Gin GoORM REST api API wrapper please:
16+
17+
* [submit a ticket to our issue tracker](https://github.com/golang-mitrah/gin-RestAPI-postgres-orm/issues)
18+
* fix the error yourself and send us a pull request
19+
20+
## **Social Media**
21+
22+
You'll find us on [Twitter](https://twitter.com/MitrahSoft) and [Facebook](http://www.facebook.com/MitrahSoft)

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module mitrahsoft.com
2+
3+
go 1.17
4+
5+
require (
6+
github.com/aws/aws-sdk-go v1.44.96
7+
github.com/gorilla/mux v1.8.0
8+
github.com/joho/godotenv v1.4.0
9+
)
10+
11+
require github.com/jmespath/go-jmespath v0.4.0 // indirect

go.sum

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
github.com/aws/aws-sdk-go v1.44.96 h1:S9paaqnJ0AJ95t5AB+iK8RM6YNZN0W0Lek1gOVJsEr8=
2+
github.com/aws/aws-sdk-go v1.44.96/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
6+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
7+
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
8+
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
9+
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
10+
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
11+
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
12+
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
13+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
14+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
16+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
17+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
18+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
19+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
22+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
23+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
24+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
25+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
27+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"os"
10+
"time"
11+
12+
"github.com/aws/aws-sdk-go/aws"
13+
"github.com/aws/aws-sdk-go/aws/credentials"
14+
"github.com/aws/aws-sdk-go/aws/session"
15+
"github.com/aws/aws-sdk-go/service/s3"
16+
"github.com/aws/aws-sdk-go/service/s3/s3manager"
17+
"github.com/gorilla/mux"
18+
"github.com/joho/godotenv"
19+
)
20+
21+
var (
22+
routerPort = ":8081"
23+
Region string
24+
Bucket string
25+
Access_Key string
26+
Secret_Key string
27+
Content_Type = "multipart/form-data"
28+
)
29+
30+
func init() {
31+
err := godotenv.Load("config.env")
32+
if err != nil {
33+
return
34+
}
35+
Region, Bucket, Access_Key, Secret_Key = os.Getenv("region"), os.Getenv("bucket"), os.Getenv("access_key"), os.Getenv("secret_key")
36+
}
37+
38+
func main() {
39+
router := mux.NewRouter()
40+
router.HandleFunc("/mitrahsoft", mitrahsoft).Methods(http.MethodPost)
41+
http.ListenAndServe(routerPort, router)
42+
}
43+
44+
func mitrahsoft(w http.ResponseWriter, r *http.Request) {
45+
// Local file
46+
byteFile, err := ioutil.ReadFile("test.txt")
47+
if ErrorCheck(w, err) {
48+
return
49+
}
50+
signedURL, err := NewUploader(r, byteFile, "./test_file/test.txt")
51+
if ErrorCheck(w, err) {
52+
return
53+
}
54+
fmt.Println("url:", signedURL)
55+
ResponseJson(w, signedURL)
56+
}
57+
58+
func NewUploader(r *http.Request, fileBytes []byte, filename string) (string, error) {
59+
sess, err := session.NewSession(&aws.Config{
60+
Region: aws.String(Region),
61+
Credentials: credentials.NewStaticCredentials(Access_Key, Secret_Key, ""),
62+
})
63+
fmt.Println("1.", err)
64+
if err != nil {
65+
return "", err
66+
}
67+
uploader := s3manager.NewUploader(sess)
68+
69+
_, err = uploader.UploadWithContext(r.Context(), &s3manager.UploadInput{
70+
Bucket: aws.String(Bucket), // bucket's name
71+
Key: aws.String("file/" + filename), // files destination location
72+
Body: bytes.NewReader(fileBytes), // content of the file
73+
ContentType: aws.String(Content_Type), // content type
74+
})
75+
fmt.Println("2.", err)
76+
if err != nil {
77+
return "", err
78+
}
79+
signedUrl, err := GetPresignedURL(sess, &Bucket, aws.String("file/"+filename))
80+
fmt.Println("4.", err)
81+
82+
if err != nil {
83+
return "", err
84+
}
85+
return signedUrl, nil
86+
}
87+
88+
func GetPresignedURL(sess *session.Session, bucket, key *string) (string, error) {
89+
svc := s3.New(sess)
90+
91+
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
92+
Bucket: bucket,
93+
Key: key,
94+
})
95+
urlStr, err := req.Presign(15 * time.Minute) // URL valid in 15 minutes
96+
fmt.Println("3.", err)
97+
if err != nil {
98+
return "", err
99+
}
100+
return urlStr, nil
101+
}
102+
103+
func ResponseJson(w http.ResponseWriter, url string) {
104+
jsn := map[string]string{
105+
"url": url,
106+
}
107+
jsnByte, _ := json.Marshal(jsn)
108+
w.Header().Set("Content-Type", "application/json")
109+
w.WriteHeader(http.StatusInternalServerError)
110+
w.Write(jsnByte)
111+
}
112+
113+
// This function is used to check error
114+
func ErrorCheck(w http.ResponseWriter, err error) bool {
115+
// Error handling
116+
if err != nil {
117+
jsn := map[string]string{
118+
"data": "Nil",
119+
"error": err.Error(),
120+
}
121+
response, _ := json.Marshal(jsn)
122+
w.Header().Set("Content-Type", "application/json")
123+
w.WriteHeader(http.StatusInternalServerError)
124+
w.Write(response)
125+
return true
126+
}
127+
return false
128+
}

test_file/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is test file.

0 commit comments

Comments
 (0)