-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
67 lines (59 loc) · 1.44 KB
/
list.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
package s3
import (
"encoding/xml"
"fmt"
"io/ioutil"
"time"
)
func (c *Client) List() ([]Object, error) {
objects := make([]Object, 0)
ctok := ""
for {
res, err := c.get(fmt.Sprintf("/?list-type=2&fetch-owner=true%s", ctok), nil)
if err != nil {
return nil, err
}
var r struct {
XMLName xml.Name `xml:"ListBucketResult"`
Next string `xml:"NextContinuationToken"`
Contents []struct {
Key string `xml:"Key"`
LastModified string `xml:"LastModified"`
ETag string `xml:"ETag"`
Size int64 `xml:"Size"`
StorageClass string `xml:"StorageClass"`
Owner struct {
ID string `xml:"ID"`
DisplayName string `xml:"DisplayName"`
} `xml:"Owner"`
} `xml:"Contents"`
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, ResponseErrorFrom(b)
}
err = xml.Unmarshal(b, &r)
if err != nil {
return nil, err
}
for _, f := range r.Contents {
mod, _ := time.Parse("2006-01-02T15:04:05.000Z", f.LastModified)
objects = append(objects, Object{
Key: f.Key,
LastModified: mod,
ETag: f.ETag[1 : len(f.ETag)-1],
Size: Bytes(f.Size),
StorageClass: f.StorageClass,
OwnerID: f.Owner.ID,
OwnerName: f.Owner.DisplayName,
})
}
if r.Next == "" {
return objects, nil
}
ctok = fmt.Sprintf("&continuation-token=%s", r.Next)
}
}