Merge pull request #3 from harshavardhana/pr_out_add_list_objects

This commit is contained in:
Harshavardhana
2015-01-17 01:05:06 -08:00
3 changed files with 60 additions and 14 deletions
+8 -4
View File
@@ -58,7 +58,7 @@ var PutObject = cli.Command{
cli.StringFlag{
Name: "body",
Value: "",
Usage: "Object blobx",
Usage: "Object blob",
},
},
}
@@ -68,6 +68,13 @@ var ListObjects = cli.Command{
Usage: "",
Description: "",
Action: doListObjects,
Flags: []cli.Flag{
cli.StringFlag{
Name: "bucket",
Value: "",
Usage: "Bucket name",
},
},
}
var ListBuckets = cli.Command{
@@ -87,9 +94,6 @@ var Configure = cli.Command{
func doListObject(c *cli.Context) {
}
func doListObjects(c *cli.Context) {
}
func doListBuckets(c *cli.Context) {
}
+43
View File
@@ -0,0 +1,43 @@
package main
import (
"errors"
"log"
"github.com/codegangsta/cli"
"github.com/minio-io/mc/pkg/s3"
)
func parseListObjectsInput(c *cli.Context) (bucket string, err error) {
bucket = c.String("bucket")
if bucket == "" {
return "", errors.New("bucket name is mandatory")
}
return bucket, nil
}
func doListObjects(c *cli.Context) {
var err error
var accessKey, secretKey, bucket string
accessKey, secretKey, err = getAWSEnvironment()
if err != nil {
log.Fatal(err)
}
bucket, err = parseListObjectsInput(c)
if err != nil {
log.Fatal(err)
}
var items []*s3.Item
s3c := s3.NewS3Client(accessKey, secretKey)
// Gets 1000 maxkeys supported with GET Bucket API
items, err = s3c.GetBucket(bucket, "", s3.MAX_OBJECT_LIST)
if err != nil {
log.Fatal(err)
}
for _, item := range items {
log.Println(item)
}
}
+9 -10
View File
@@ -36,7 +36,9 @@ import (
"time"
)
const maxList = 1000
const (
MAX_OBJECT_LIST = 1000
)
// Client is an Amazon S3 client.
type Client struct {
@@ -189,20 +191,19 @@ func (c *Client) BucketLocation(bucket string) (location string, err error) {
return "s3-" + xres.Location + ".amazonaws.com", nil
}
// ListBucket returns 0 to maxKeys (inclusive) items from the provided
// bucket. Keys before startAt will be skipped. (This is the S3
// GetBucket (List Objects) returns 0 to maxKeys (inclusive) items from the
// 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) ListBucket(bucket string, startAt string, maxKeys int) (items []*Item, err error) {
// 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) {
if maxKeys < 0 {
return nil, errors.New("invalid negative maxKeys")
}
marker := startAt
for len(items) < maxKeys {
fetchN := maxKeys - len(items)
if fetchN > maxList {
fetchN = maxList
if fetchN > MAX_OBJECT_LIST {
fetchN = MAX_OBJECT_LIST
}
var bres listBucketResults
@@ -390,8 +391,6 @@ func IsValidBucket(bucket string) bool {
}
// Error is the type returned by some API operations.
//
// TODO: it should be more/all of them.
type Error struct {
Op string
Code int // HTTP status code