forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 17
/
delete_multiple.go
101 lines (84 loc) · 2.16 KB
/
delete_multiple.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
package s3gof3r
import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/xml"
"io/ioutil"
"net/http"
"github.com/github/s3gof3r/internal/s3client"
)
type deleteObject struct {
Key string `xml:"Key"`
VersionId string `xml:"VersionId,omitempty"`
}
type deleteRequest struct {
XMLName xml.Name `xml:"Delete"`
Objects []deleteObject `xml:"Object"`
Quiet bool `xml:"Quiet"`
}
type DeletedObject struct {
Key string `xml:"Key"`
VersionId string `xml:"VersionId"`
DeleteMarker bool `xml:"DeleteMarker"`
DeleteMarkerVersionId string `xml:"DeleteMarkerVersionId"`
}
type DeleteError struct {
Key string `xml:"Key"`
Code string `xml:"Code"`
Message string `xml:"Message"`
}
type DeleteResult struct {
XMLName xml.Name `xml:"DeleteResult"`
Deleted []DeletedObject `xml:"Deleted"`
Errors []DeleteError `xml:"Error"`
}
func deleteMultiple(c *Config, b *Bucket, quiet bool, keys []string) (DeleteResult, error) {
if len(keys) == 0 {
return DeleteResult{}, nil
}
u, err := b.url("", c)
if err != nil {
return DeleteResult{}, err
}
u.RawQuery = "delete"
objects := make([]deleteObject, 0, len(keys))
for _, key := range keys {
objects = append(objects, deleteObject{Key: key})
}
deleteRequest := deleteRequest{
Objects: objects,
Quiet: quiet,
}
body, err := xml.Marshal(deleteRequest)
if err != nil {
return DeleteResult{}, err
}
md5sum := md5.Sum(body)
r := http.Request{
Method: "POST",
URL: u,
Body: ioutil.NopCloser(bytes.NewReader(body)),
ContentLength: int64(len(body)),
Header: make(http.Header),
}
r.Header.Set(s3client.MD5Header, base64.StdEncoding.EncodeToString(md5sum[:]))
b.Sign(&r)
resp, err := b.conf().Do(&r)
if err != nil {
return DeleteResult{}, err
}
if resp.StatusCode != 200 {
return DeleteResult{}, s3client.NewRespError(resp)
}
var result DeleteResult
decoder := xml.NewDecoder(resp.Body)
if err := decoder.Decode(&result); err != nil {
_ = resp.Body.Close()
return DeleteResult{}, err
}
if err := resp.Body.Close(); err != nil {
return DeleteResult{}, err
}
return result, nil
}