-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (78 loc) · 1.72 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
101
102
103
104
105
package s3_all_delete
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// ----------------------------------------------------
// 全体変数
// ----------------------------------------------------
var (
)
/*
@brief エントリポイント
@param -
@return -
@note
@exception
-
@history
*/
// Handle xxx
func s3_all_delete(s3bucket string) {
//-----------------------------------------------
// S3からファイル取得
//-----------------------------------------------
//awsセッション
sess := session.Must(session.NewSession())
// S3 clientを作成
s3Client := s3.New(sess)
// ファイルの存在確認
loo, err := s3Client.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(s3bucket),
//Prefix: aws.String("/"),
})
if err != nil {
return
}
// データが存在しなかった場合
if *loo.KeyCount == 0 {
return
}
for _, item := range loo.Contents {
key := *item.Key
if !strings.Contains(key, ".bin") {
continue
}
// Object取得
_, err := s3Client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s3bucket),
Key: aws.String(key),
})
if err != nil {
return
}
err = deleteS3object(key,s3bucket)
if err != nil {
return
}
}
return
}
func deleteS3object(key string,s3bucket string) error {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// S3 clientを作成
s3Client := s3.New(sess)
_, err := s3Client.DeleteObject(
&s3.DeleteObjectInput{
Bucket: aws.String(s3bucket),
Key: aws.String(key),
})
if err != nil {
return err
}
return nil
}