-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
71 lines (66 loc) · 1.7 KB
/
file.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
package s3
import (
"net/url"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/samber/lo"
)
func (s *Client) FileList(bucket, path string) ([]Object, error) {
key := strings.TrimPrefix(path, "/")
objects := make([]Object, 0)
err := s.client.ListObjectsPages(&s3.ListObjectsInput{
Bucket: &bucket,
Prefix: &key,
MaxKeys: aws.Int64(1000),
}, func(page *s3.ListObjectsOutput, lp bool) bool {
objs := lo.Map(page.Contents, func(item *s3.Object, i int) Object {
return Object{Key: *item.Key, Size: *item.Size}
})
objects = append(objects, objs...)
if len(objs) < 1000 {
return false
}
return !lp
})
return objects, err
}
func (s *Client) FileListV2(bucket, path string) ([]Object, error) {
key := strings.TrimPrefix(path, "/")
objects := make([]Object, 0)
err := s.client.ListObjectsV2Pages(&s3.ListObjectsV2Input{
Bucket: &bucket,
Prefix: &key,
MaxKeys: aws.Int64(1000),
}, func(page *s3.ListObjectsV2Output, lp bool) bool {
objs := lo.Map(page.Contents, func(item *s3.Object, i int) Object {
return Object{Key: *item.Key, Size: *item.Size}
})
objects = append(objects, objs...)
if len(objs) < 1000 {
return false
}
return !lp
})
return objects, err
}
func (s *Client) Link(bucket, key string, expire time.Duration) (string, error) {
key = strings.TrimPrefix(key, "/")
req, _ := s.client.GetObjectRequest(&s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
link, err := req.Presign(expire)
if err != nil {
return "", err
}
u, _ := url.Parse(link)
if s.removeBucket {
u.Path = strings.Replace(u.Path, "/"+bucket, "", 1)
}
if s.customHost != "" {
u.Host = s.customHost
}
return u.String(), nil
}