Add support for marker, delimiter, prefix
This commit is contained in:
+30
-9
@@ -222,11 +222,16 @@ func (a BySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a BySize) Less(i, j int) bool { return a[i].Size < a[j].Size }
|
||||
|
||||
type listBucketResults struct {
|
||||
Contents []*Item
|
||||
IsTruncated bool
|
||||
MaxKeys int
|
||||
Name string // bucket name
|
||||
Marker string
|
||||
Contents []*Item
|
||||
IsTruncated bool
|
||||
MaxKeys int
|
||||
Name string // bucket name
|
||||
Marker string
|
||||
Delimiter string
|
||||
Prefix string
|
||||
CommonPrefixes struct {
|
||||
Prefix string
|
||||
}
|
||||
}
|
||||
|
||||
// BucketLocation returns the S3 endpoint to be used with the given bucket.
|
||||
@@ -255,10 +260,14 @@ func (c *Client) BucketLocation(bucket string) (location string, err error) {
|
||||
// provided bucket. Keys before startAt will be skipped. (This is the S3
|
||||
// 'marker' value). If the length of the returned items is equal to
|
||||
// maxKeys, there is no indication whether or not the returned list is truncated.
|
||||
func (c *Client) GetBucket(bucket string, startAt string, maxKeys int) (items []*Item, err error) {
|
||||
func (c *Client) GetBucket(bucket string, startAt, prefix, delimiter string, maxKeys int) (items []*Item, err error) {
|
||||
var url_ string
|
||||
var buffer bytes.Buffer
|
||||
|
||||
if maxKeys < 0 {
|
||||
return nil, errors.New("invalid negative maxKeys")
|
||||
}
|
||||
|
||||
marker := startAt
|
||||
for len(items) < maxKeys {
|
||||
fetchN := maxKeys - len(items)
|
||||
@@ -266,10 +275,19 @@ func (c *Client) GetBucket(bucket string, startAt string, maxKeys int) (items []
|
||||
fetchN = MAX_OBJECT_LIST
|
||||
}
|
||||
var bres listBucketResults
|
||||
buffer.WriteString(fmt.Sprintf("%s?max-keys=%d", c.bucketURL(bucket), fetchN))
|
||||
switch true {
|
||||
case marker != "":
|
||||
buffer.WriteString(fmt.Sprintf("&marker=%s", url.QueryEscape(marker)))
|
||||
fallthrough
|
||||
case prefix != "":
|
||||
buffer.WriteString(fmt.Sprintf("&prefix=%s", url.QueryEscape(prefix)))
|
||||
fallthrough
|
||||
case delimiter != "":
|
||||
buffer.WriteString(fmt.Sprintf("&delimiter=%s", url.QueryEscape(delimiter)))
|
||||
}
|
||||
|
||||
url_ := fmt.Sprintf("%s?marker=%s&max-keys=%d",
|
||||
c.bucketURL(bucket), url.QueryEscape(marker), fetchN)
|
||||
|
||||
url_ = buffer.String()
|
||||
// Try the enumerate three times, since Amazon likes to close
|
||||
// https connections a lot, and Go sucks at dealing with it:
|
||||
// https://code.google.com/p/go/issues/detail?id=3514
|
||||
@@ -334,6 +352,9 @@ func (c *Client) GetBucket(bucket string, startAt string, maxKeys int) (items []
|
||||
// log.Printf("Not truncated. so breaking. items = %d; len Contents = %d, url = %s", len(items), len(bres.Contents), url_)
|
||||
break
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, errors.New("No items replied")
|
||||
}
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
+13
-13
@@ -33,6 +33,10 @@ const (
|
||||
printFormat = "2006-01-02 15:04:05"
|
||||
)
|
||||
|
||||
const (
|
||||
delimiter = "/"
|
||||
)
|
||||
|
||||
func parseTime(t string) string {
|
||||
ti, _ := time.Parse(recvFormat, t)
|
||||
return ti.Format(printFormat)
|
||||
@@ -61,18 +65,14 @@ func printObject(v int64, date, key string) {
|
||||
}
|
||||
|
||||
func getBucketAndObject(p string) (bucket, object string) {
|
||||
parts := strings.Split(path.Clean(p), "/")
|
||||
switch true {
|
||||
case len(parts) == 2:
|
||||
bucket = parts[0]
|
||||
object = parts[1]
|
||||
case len(parts) < 2:
|
||||
bucket = parts[0]
|
||||
object = ""
|
||||
case len(parts) > 2:
|
||||
bucket = parts[0]
|
||||
for _, v := range parts[1 : len(parts)-1] {
|
||||
object = object + "/" + v
|
||||
i := strings.Index(p, delimiter)
|
||||
if i == -1 {
|
||||
bucket = p
|
||||
} else if i != -1 {
|
||||
bucket = p[:i]
|
||||
object = path.Base(p)
|
||||
if bucket == object {
|
||||
object = ""
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -97,7 +97,7 @@ func doFsList(c *cli.Context) {
|
||||
}
|
||||
bucket, object := getBucketAndObject(input)
|
||||
if object == "" {
|
||||
items, err = s3c.GetBucket(bucket, "", s3.MAX_OBJECT_LIST)
|
||||
items, err = s3c.GetBucket(bucket, "", "", "", s3.MAX_OBJECT_LIST)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user