/
main.go
54 lines (45 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"bytes"
"io"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/textract"
"github.com/gin-gonic/gin"
)
func main() {
// Set up the Textract client
// session will automatically reads your AWS credentials from ~/.aws/credentials
// to manually set the creds, use: Credentials: credentials.NewStaticCredentials
// inside the aws.Config{} struct
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("ap-southeast-1"),
},
SharedConfigState: session.SharedConfigEnable,
}))
textractClient := textract.New(sess)
r := gin.Default()
r.GET("/analyze", func(c *gin.Context) {
imageFile, _, err := c.Request.FormFile("image")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
defer imageFile.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, imageFile); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Analyze the image with Textract
resp, err := AnalyzeImage(textractClient, buf.Bytes())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, resp)
})
r.Run("127.0.0.1:8080")
}