/
common.go
206 lines (173 loc) · 4.92 KB
/
common.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package s3
import (
"context"
"io/fs"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/wweir/mafia/drivers"
"github.com/wweir/mafia/pkg/fsmock"
"github.com/wweir/mafia/pkg/fspath"
)
var _ drivers.FSAdaptor = new(common)
type common struct {
*s3.Client
timeout time.Duration
drivers.MockFSFull
}
func newCommon(timout time.Duration) (*common, error) {
ctx, cancel := context.WithTimeout(context.Background(), timout)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("cn-northwest-1"))
if err != nil {
return nil, err
}
return &common{
timeout: timout,
Client: s3.NewFromConfig(cfg),
}, nil
}
func (c *common) DeleteDir(path string) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
bucket, object := fspath.SplitPrefixDir(path)
if object == "" {
log.Info().Interface("bucket", bucket).Msg("delete")
// _, err := c.Client.DeleteBucket(ctx, &s3.DeleteBucketInput{
// Bucket: aws.String(bucket),
// })
return nil
}
p := s3.NewListObjectsV2Paginator(c.Client, &s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(object + "/"),
FetchOwner: true,
})
files := []types.ObjectIdentifier{{Key: aws.String(object + "/")}}
for p.HasMorePages() {
out, err := p.NextPage(ctx)
if err != nil {
return errors.WithStack(err)
}
for _, content := range out.Contents {
files = append(files, types.ObjectIdentifier{
Key: content.Key,
})
}
}
log.Info().Interface("files", files).Msg("delete")
// _, err := c.Client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
// Bucket: aws.String(bucket),
// Delete: &types.Delete{
// Objects: files,
// },
// })
return nil
}
func (c *common) DeleteFile(path string) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
bucket, object := fspath.SplitPrefixDir(path)
_, err := c.Client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
})
return err
}
func (c *common) Rename(fromPath string, toPath string) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
c.Client.CopyObject(ctx, &s3.CopyObjectInput{})
fromBucket, fromObject := fspath.SplitPrefixDir(fromPath)
toBucket, toObject := fspath.SplitPrefixDir(toPath)
switch "" {
case fromBucket, toBucket:
return errors.New("bucket name must be setted")
case fromObject, toObject:
return errors.New("object name must be setted")
default:
if fromBucket != toBucket {
return errors.Errorf("not impliment")
}
return nil
}
}
func (c *common) Mkdir(name string, perm os.FileMode) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
bucket, object := fspath.SplitPrefixDir(name)
if object == "" {
_, err := c.Client.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
return err
}
return nil
// c.Client.PutObject(ctx,&s3.PutObjectInput{
// })
}
func (c *common) Stat(name string) (os.FileInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
bucket, object := fspath.SplitPrefixDir(name)
if object == "" {
return fsmock.MockFileInfo(name, true, 0, nil), nil
}
out, err := c.Client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
})
if err != nil {
return fsmock.MockFileInfo(name, true, 0, nil), nil
}
return fsmock.MockFileInfo(name, false, out.ContentLength, out.LastModified), nil
}
func (c *common) ReadDir(name string) ([]fs.FileInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
bucket, object := fspath.SplitPrefixDir(name)
if bucket == "" {
out, err := c.Client.ListBuckets(ctx, &s3.ListBucketsInput{})
if err != nil {
return nil, errors.WithStack(err)
}
fis := make([]fs.FileInfo, 0, len(out.Buckets))
for b := range out.Buckets {
fis = append(fis, fsmock.MockFileInfo(*out.Buckets[b].Name, true, 0, nil))
}
return fis, nil
}
if object != "" {
object += "/"
}
p := s3.NewListObjectsV2Paginator(c.Client, &s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(object),
Delimiter: aws.String("/"),
FetchOwner: true,
})
var fis = []fs.FileInfo{}
for p.HasMorePages() {
out, err := p.NextPage(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
for p := range out.CommonPrefixes {
fis = append(fis, fsmock.MockFileInfo(
*out.CommonPrefixes[p].Prefix, true, 0, nil))
}
for c := range out.Contents {
content := out.Contents[c]
if *content.Key != object {
fis = append(fis, fsmock.MockFileInfo(
*content.Key, false, content.Size, content.LastModified))
}
}
}
return fis, nil
}