-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
448 lines (384 loc) · 9.52 KB
/
s3.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package aws
import (
"errors"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
type s3Config struct {
host string
accessKey string
secretKey string
forcePathStyle bool
}
type DownloadInfo struct {
DeleteInfo
LocalPrefix string
}
type DeleteInfo struct {
Bucket string
Key string
}
type Empty struct {
}
var (
s3Client *s3.S3
s3Session *session.Session
ErrDataNotFound = errors.New("data not found")
// "face_prod_daily_data_20200401"
host = "http://s3-prod.xxx:7480"
accessKey = "accessKey"
secretKey = "secretKey"
)
// initialize ceph client for specific city
func Init() {
log.Println("ceph.Init start")
s3Session = session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("default"),
Endpoint: &host,
S3ForcePathStyle: aws.Bool(true),
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
},
}))
s3Client = s3.New(s3Session)
}
func AllBuckets() {
outPut, err := s3Client.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
log.Println(err)
return
}
log.Println(len(outPut.Buckets))
}
func BucketList(bucketName string) {
var keyMap = make(map[string]Empty)
var keyList = make([]string, 0)
var listInput = s3.ListObjectsInput{
Bucket: &bucketName,
}
var err error
var outPut *s3.ListObjectsOutput
for {
outPut, err = s3Client.ListObjects(&listInput)
if err != nil {
log.Println(err)
}
for _, content := range outPut.Contents {
keyMap[*content.Key] = Empty{}
keyList = append(keyList, *content.Key)
}
if outPut.NextMarker == nil || !*outPut.IsTruncated || outPut.MaxKeys == nil || *outPut.MaxKeys == 0 {
break
}
listInput.Marker = outPut.NextMarker
}
log.Println("keyMap", len(keyMap))
log.Println("keyList", len(keyList))
}
func DownloadBucket(processor int, bucket, imageDir string) error {
log.Println("ceph.DownloadBucket start")
// 当前目录以 Bucket 命名
if _, err := os.Stat(imageDir); err != nil {
if !os.IsNotExist(err) {
return err
}
if err = os.MkdirAll(imageDir, 0755); err != nil {
return err
}
}
downloadChan := make(chan DownloadInfo)
var wg sync.WaitGroup
wg.Add(processor)
for i := 0; i < processor; i++ {
go func(idx int) {
log.Println("start multipleDownload", idx)
multipleDownload(downloadChan)
wg.Done()
log.Println("finish multipleDownload", idx)
}(i)
}
var (
num = 0
err error
output *s3.ListObjectsOutput
input = s3.ListObjectsInput{
Bucket: &bucket,
}
)
// 遍历 Bucket 时下载图片
for {
output, err = s3Client.ListObjects(&input)
if err != nil {
log.Println(err)
return err
}
for _, content := range output.Contents {
num++
if num%100000 == 0 {
log.Println("download object", num)
}
downloadChan <- DownloadInfo{
DeleteInfo: DeleteInfo{
Bucket: bucket,
Key: *content.Key,
},
LocalPrefix: filepath.Join(imageDir, *content.Key),
}
}
// 没有偏移量了; 此次返回没有截取; MaxKeys 无效
if output.NextMarker == nil || !*output.IsTruncated || output.MaxKeys == nil || *output.MaxKeys == 0 {
break
}
// ceph 每次最多返回 1000 个内容
// 用 marker 标记偏移量
input.Marker = output.NextMarker
}
close(downloadChan)
wg.Wait()
log.Println("ceph.DownloadBucket finish", num)
return nil
}
func multipleDownload(dc chan DownloadInfo) {
for dInfo := range dc {
if err := downloadImage(dInfo.Bucket, dInfo.Key, dInfo.LocalPrefix, false); err != nil {
// 出错了就停止所有的协程, 让主函数停止, 抛出错误
close(dc)
panic(err)
}
}
}
func multipleDelete(dc chan DeleteInfo) {
for dInfo := range dc {
if err := DeleteObject(dInfo.Bucket, dInfo.Key); err != nil {
// 出错了就停止所有的协程, 让主函数停止, 抛出错误
close(dc)
panic(err)
}
}
}
// 下载 ceph 图片数据到本地
// city : ceph 数据所在的集群
// date : 数据日期, 按照日期区分 Bucket
// remoteName : ceph 数据的 full path name, 不包括 Bucket
// localName : 下载到本地的路径及名称
// force : 是否强制覆盖本地数据(若本地同名文件存在)
func downloadImage(bucketName, key, prefixName string, force bool) error {
// remoteName 的文件数据以二进制留的形式存在 resp.Body
resp, err := s3Client.GetObject(&s3.GetObjectInput{
Bucket: &bucketName,
Key: &key,
})
if err != nil {
// 类型转换会丢数据, 等到升级 v2 版本再试试
if cephErr := err.(awserr.Error); cephErr.Code() == "NotFound" {
return ErrDataNotFound
} else {
return err
}
}
defer func() {
_ = resp.Body.Close()
}()
localName := prefixName + getSuffix(*resp.ContentType)
if _, err := os.Stat(localName); err == nil {
if !force {
// 不强制覆盖
return nil
}
// 强制覆盖就删掉已有文件
if err = os.Remove(localName); err != nil {
return err
}
} else if !os.IsNotExist(err) {
// 嗅探本地文件出错
return err
}
// 运行到这里, 本地文件应该不存在
// 准备接收文件
idx := strings.LastIndex(localName, "/")
if idx > 0 {
dirPath := localName[0:idx]
err := os.MkdirAll(dirPath, 0755)
if err != nil {
return err
}
}
var localFile *os.File
if localFile, err = os.OpenFile(localName, os.O_CREATE|os.O_RDWR, 0644); err != nil {
return err
}
defer func() {
_ = localFile.Close()
}()
// 将 remote 的数据流 copy 到 local
if _, err = io.Copy(localFile, resp.Body); err != nil {
return err
}
return nil
}
func newS3Client(config s3Config) *s3.S3 {
return s3.New(session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("default"),
Endpoint: &config.host,
S3ForcePathStyle: &config.forcePathStyle,
Credentials: credentials.NewStaticCredentials(config.accessKey, config.secretKey, ""),
},
})))
}
func getSuffix(cType string) string {
switch cType {
case "image/jpeg":
return ".jpg"
}
panic(cType)
}
func DownloadObject(sess *session.Session, filename, key, bucket *string) error {
// snippet-start:[s3.go.download_object.create]
file, err := os.Create(*filename)
// snippet-end:[s3.go.download_object.create]
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()
// snippet-start:[s3.go.download_object.call]
downloader := s3manager.NewDownloader(sess)
_, err = downloader.Download(file,
&s3.GetObjectInput{
Bucket: bucket,
Key: key,
})
// snippet-end:[s3.go.download_object.call]
if err != nil {
return err
}
return nil
}
func DeleteBucket(processor int, bucket string) error {
log.Println("ceph.DeleteBucket start", bucket)
var (
num = 0
err error
output *s3.ListObjectsOutput
input = s3.ListObjectsInput{
Bucket: &bucket,
}
)
deleteChan := make(chan DeleteInfo)
var wg sync.WaitGroup
wg.Add(processor)
for i := 0; i < processor; i++ {
go func(idx int) {
log.Println("start multipleDelete", idx)
multipleDelete(deleteChan)
wg.Done()
log.Println("finish multipleDelete", idx)
}(i)
}
// 遍历 Bucket 删除
for {
output, err = s3Client.ListObjects(&input)
if err != nil {
log.Println(err)
return err
}
if len(output.Contents) == 0 {
break
}
for _, content := range output.Contents {
num++
if num%100000 == 0 {
log.Println("delete object", num)
}
deleteChan <- DeleteInfo{
Bucket: bucket,
Key: *content.Key,
}
}
}
wg.Wait()
// Delete the S3 Bucket
// It must be empty or else the call fails
if _, err = s3Client.DeleteBucket(&s3.DeleteBucketInput{Bucket: aws.String(bucket)}); err != nil {
return err
}
// Wait until Bucket is deleted before finishing
if err = s3Client.WaitUntilBucketNotExists(&s3.HeadBucketInput{Bucket: aws.String(bucket)}); err != nil {
return err
}
log.Println("ceph.DeleteBucket finish", bucket)
return nil
}
func DeleteObject(bucket, key string) error {
// Delete the item
_, err := s3Client.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(key)})
if err != nil {
return err
}
err = s3Client.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return err
}
return nil
}
func Count(bucket string) (int, error) {
log.Println("ceph.Count start")
var (
num = 0
err error
output *s3.ListObjectsOutput
input = s3.ListObjectsInput{
Bucket: &bucket,
}
)
// 遍历 Bucket 计数
for {
output, err = s3Client.ListObjects(&input)
if err != nil {
log.Println(err)
return 0, err
}
for range output.Contents {
num++
if num%100000 == 0 {
log.Println("count", num)
}
}
// 没有偏移量了; 此次返回没有截取; MaxKeys 无效
if output.NextMarker == nil || !*output.IsTruncated || output.MaxKeys == nil || *output.MaxKeys == 0 {
break
}
// ceph 每次最多返回 1000 个内容
// 用 marker 标记偏移量
input.Marker = output.NextMarker
}
log.Println("ceph.Count finish", num)
return num, nil
}
func ExistBucket(bucket string) (bool, error) {
_, err := s3Client.HeadBucket(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
if cephErr := err.(awserr.Error); cephErr.Code() == "NotFound" {
return false, nil
}
return false, err
}
return true, nil
}