|
| 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 | +} |
0 commit comments