/
service.go
54 lines (45 loc) · 1.17 KB
/
service.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 image
import (
"context"
"io"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
bucket = os.Getenv("S3_BUCKET_NAME")
key = os.Getenv("S3_SECRET_KEY")
)
type Service interface {
UploadToS3(ctx context.Context, readSeeker io.ReadSeeker, filename string) (string, error)
}
type imageService struct {
awsSession *session.Session
}
func NewService(awsSession *session.Session) Service {
return &imageService{
awsSession: awsSession,
}
}
func (s *imageService) UploadToS3(ctx context.Context, readSeeker io.ReadSeeker, filename string) (string, error) {
svc := s3.New(s.awsSession)
// This uploads the contents of the buffer to S3
_, err := svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
ACL: aws.String("public-read"),
Body: readSeeker,
ContentType: aws.String("image/jpeg"),
})
if err != nil {
return "", err
}
params := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
}
req, _ := svc.GetObjectRequest(params)
req.Build()
return req.HTTPRequest.URL.String(), nil
}