-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_content.go
71 lines (52 loc) · 1.78 KB
/
remove_content.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 upload
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
var _svc *s3.S3
var _bucketName string
/**
* Remove all content from the Bucket
*/
func RemoveContent(svc *s3.S3, bucketName string) {
_svc = svc
_bucketName = bucketName
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucketName)})
ExitErrorf("Unable to list items in bucket %q, %v", err, bucketName)
num_objs := len(resp.Contents)
// Create Delete object with slots for the objects to delete
var objs = make([]*s3.ObjectIdentifier, num_objs)
for i, o := range resp.Contents {
objs[i] = &s3.ObjectIdentifier{Key: aws.String(*o.Key)}
removeObject(objs[i])
}
}
//
//
func removeObject(o *s3.ObjectIdentifier) {
_, err := _svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(_bucketName), Key: o.Key})
ExitErrorf("%v - Unable to delete object %q from bucket %q", err, o, _bucketName)
fmt.Println("Item removed: ", *o.Key)
}
/**
*
*/
func ClearBucket(svc *s3.S3, bucketName string) {
_svc = svc
_bucketName = bucketName
resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucketName)})
ExitErrorf("Unable to list items in bucket:", err, bucketName)
num_objs := len(resp.Contents)
// Create Delete object with slots for the objects to delete
var items s3.Delete
var objs = make([]*s3.ObjectIdentifier, num_objs)
for i, o := range resp.Contents {
objs[i] = &s3.ObjectIdentifier{Key: aws.String(*o.Key)}
}
// Add list of objects to delete to Delete object
items.SetObjects(objs)
svc.DeleteObjects(&s3.DeleteObjectsInput{Bucket: aws.String(_bucketName), Delete: &items})
// ExitErrorf("Unable to delete objects from bucket %q, %v", err, bucketName)
fmt.Println("Deleted", num_objs, "object(s) from bucket", bucketName)
}