-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
83 lines (60 loc) · 1.66 KB
/
files.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
package s3
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/vanclief/ez"
)
func (c *Client) ListFiles(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) {
const op = "Client.ListFiles"
input.Bucket = aws.String(c.Bucket)
objects, err := c.s3.ListObjects(input)
if err != nil {
return nil, ez.Wrap(op, err)
}
return objects, nil
}
func (c *Client) UploadFile(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
const op = "Client.UploadFile"
input.Bucket = aws.String(c.Bucket)
res, err := c.s3.PutObject(input)
if err != nil {
return nil, ez.Wrap(op, err)
}
return res, nil
}
func (c *Client) DeleteFile(input *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error) {
const op = "Client.DeleteFile"
input.Bucket = aws.String(c.Bucket)
result, err := c.s3.DeleteObject(input)
if err != nil {
return nil, ez.Wrap(op, err)
}
return result, nil
}
func (c *Client) FileExists(input *s3.HeadObjectInput) (bool, error) {
const op = "Client.FileExists"
input.Bucket = aws.String(c.Bucket)
_, err := c.s3.HeadObject(input)
if err != nil {
awsErr, ok := err.(awserr.Error)
if ok {
if awsErr.Code() == s3.ErrCodeNoSuchKey || awsErr.Code() == "NotFound" {
return false, nil
}
}
return false, ez.Wrap(op, err)
}
return true, nil
}
func (c *Client) GetPrivateURL(input *s3.GetObjectInput) (string, error) {
const op = "Client.GetPrivateURL"
input.Bucket = aws.String(c.Bucket)
req, _ := c.s3.GetObjectRequest(input)
urlStr, err := req.Presign(1440 * time.Minute)
if err != nil {
return "", ez.Wrap(op, err)
}
return urlStr, nil
}