-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (82 loc) · 2 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bytes"
"flag"
"fmt"
"path"
"io/ioutil"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/google/uuid"
)
func prefix() string {
return uuid.New().String()
}
func upload(bucket, fileName string) (string, error) {
// Generated Prefix is used to prevent name collisions
s3Key := prefix() + "/" + path.Base(fileName)
body, err := ioutil.ReadFile(fileName)
if err != nil {
return s3Key, err
}
sess, err := session.NewSession()
if err != nil {
return s3Key, err
}
s3Service := s3.New(sess)
_, err = s3Service.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(s3Key),
Body: bytes.NewReader(body),
})
return s3Key, err
}
func createPresignedUrl(bucket, keyName string, valid int) (string, error) {
sess, err := session.NewSession()
if err != nil {
return "", err
}
s3Service := s3.New(sess)
req, _ := s3Service.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(keyName),
})
duration, err := time.ParseDuration(fmt.Sprintf("%dm", valid))
if err != nil {
return "", err
}
return req.Presign(duration)
}
func errAndExit(msg string) {
fmt.Fprint(os.Stderr, msg + "\n")
os.Exit(1)
}
func main() {
var valid = flag.Int("valid", 15, "How long the Presigned URL is valid for in minutes.")
var bucketName = flag.String("bucket", "", "Bucket to use for file sharing. GRAEAE_BUCKET")
flag.Parse()
// Check for bucket name
if *bucketName == "" {
*bucketName = os.Getenv("GRAEAE_BUCKET")
if *bucketName == "" {
errAndExit("Must provide a bucket name to share file with.")
}
}
args := flag.Args()
if len(args) == 0 {
errAndExit("Missing file to share")
}
// Upload the Object to S3
s3Key, err := upload(*bucketName, flag.Arg(0))
if err != nil {
errAndExit(err.Error())
}
signedUrl, err := createPresignedUrl(*bucketName, s3Key, *valid)
if err != nil {
errAndExit(err.Error())
}
fmt.Println(signedUrl)
}