diff --git a/cmd-mb.go b/cmd-mb.go index 593b6b60..fb8e91cf 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -29,7 +29,7 @@ func doMakeBucketCmd(c *cli.Context) { } s3c, err := getNewClient(c, args.source.url) - if !s3.IsValidBucket(args.source.bucket) { + if !s3.IsValidBucketName(args.source.bucket) { fatal(errInvalidbucket.Error()) } diff --git a/cmd-options.go b/cmd-options.go index 3585ca3a..fb910ace 100644 --- a/cmd-options.go +++ b/cmd-options.go @@ -89,10 +89,12 @@ var donutCmd = cli.Command{ } type object struct { + scheme string // protocol type: possible values are http, https, donut, nil + host string + // Bucket name can also be a DNS name. "." is allowed, with certain restrictions. + // Read more at http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html bucket string key string - host string - scheme string url *url.URL } diff --git a/cmd-recursive-cp.go b/cmd-recursive-cp.go index 518e8bec..1d34733d 100644 --- a/cmd-recursive-cp.go +++ b/cmd-recursive-cp.go @@ -66,24 +66,10 @@ func (w *walk) putWalk(p string, i os.FileInfo, err error) error { return nil } -// isValidBucketName checks for valid bucket naming convention. Remember, bucket name is also used a DNS hostname. -func isValidBucketName(p string) error { - if path.IsAbs(p) { - return errors.New("directory bucketname cannot be absolute") - } - if strings.HasPrefix(p, "..") { - return errors.New("Relative directory references not supported") - } - if !s3.IsValidBucket(p) { - return errInvalidbucket - } - return nil -} - -// isBucketExists checks if a bucket exists -func isBucketExists(name string, v []*client.Bucket) bool { +// isBucketExist checks if a bucket exists +func isBucketExist(bucketName string, v []*client.Bucket) bool { for _, b := range v { - if name == b.Name { + if bucketName == b.Name { return true } } @@ -99,8 +85,8 @@ func doRecursiveCP(c *cli.Context, args *cmdArgs) error { switch true { case args.source.bucket == "": input := path.Clean(args.source.key) - if err := isValidBucketName(input); err != nil { - return err + if s3.IsValidBucketName(input) { + return fmt.Errorf("Invalid input bucket name [%s]", input) } st, err = os.Stat(input) if os.IsNotExist(err) { @@ -115,7 +101,7 @@ func doRecursiveCP(c *cli.Context, args *cmdArgs) error { } p := &walk{s3c, args} buckets, err = s3c.ListBuckets() - if !isBucketExists(args.destination.bucket, buckets) { + if !isBucketExist(args.destination.bucket, buckets) { // Create bucketname, before uploading files err = s3c.PutBucket(args.destination.bucket) if err != nil { diff --git a/cmd-colors.go b/colors.go similarity index 100% rename from cmd-colors.go rename to colors.go diff --git a/pkg/s3/bucket.go b/pkg/s3/bucket.go index 7a21dd82..2553d343 100644 --- a/pkg/s3/bucket.go +++ b/pkg/s3/bucket.go @@ -85,7 +85,7 @@ func (c *s3Client) ListBuckets() ([]*client.Bucket, error) { // PutBucket - create new bucket func (c *s3Client) PutBucket(bucket string) error { var url string - if IsValidBucket(bucket) && !strings.Contains(bucket, ".") { + if IsValidBucketName(bucket) && !strings.Contains(bucket, ".") { url = fmt.Sprintf("%s://%s/%s", c.URL.Scheme, c.URL.Host, bucket) } req := newReq(url) diff --git a/pkg/s3/client.go b/pkg/s3/client.go index 6bb24e0f..7e8cdc6f 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -82,10 +82,14 @@ func GetNewClient(auth *client.Auth, u *url.URL, transport http.RoundTripper) cl }} } -// bucketURL returns the URL prefix of the bucket, with trailing slash +// bucketURL constructs a URL (with a trailing slash) for a given +// bucket. URL is appropriately encoded based on the host's object +// storage implementation. func (c *s3Client) bucketURL(bucket string) string { var url string - if IsValidBucket(bucket) && !strings.Contains(bucket, ".") { + + // TODO: Bucket names can contain ".". This second check should be removed. + if IsValidBucketName(bucket) && !strings.Contains(bucket, ".") { // if localhost use PathStyle if strings.Contains(c.URL.Host, "localhost") || strings.Contains(c.URL.Host, "127.0.0.1") { return fmt.Sprintf("%s://%s/%s", c.URL.Scheme, c.URL.Host, bucket) @@ -101,6 +105,7 @@ func (c *s3Client) bucketURL(bucket string) string { return url } +// keyURL constructs a URL using bucket and object key func (c *s3Client) keyURL(bucket, key string) string { url := c.bucketURL(bucket) if strings.Contains(c.URL.Host, "localhost") || strings.Contains(c.URL.Host, "127.0.0.1") { @@ -116,6 +121,8 @@ func (c *s3Client) keyURL(bucket, key string) string { func newReq(url string) *http.Request { req, err := http.NewRequest("GET", url, nil) if err != nil { + // TODO: never exit from inside a package. Let the + // caller handle errors gracefully. panic(fmt.Sprintf("s3 client; invalid URL: %v", err)) } req.Header.Set("User-Agent", "Minio s3Client") @@ -135,9 +142,9 @@ func parseListAllMyBuckets(r io.Reader) ([]*client.Bucket, error) { return res.Buckets.Bucket, nil } -// IsValidBucket reports whether bucket is a valid bucket name, per Amazon's naming restrictions. +// IsValidBucketName reports whether bucket is a valid bucket name, per Amazon's naming restrictions. // See http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html -func IsValidBucket(bucket string) bool { +func IsValidBucketName(bucket string) bool { if len(bucket) < 3 || len(bucket) > 63 { return false } diff --git a/pkg/s3/client_test.go b/pkg/s3/client_test.go index 40ef595b..ba1033ee 100644 --- a/pkg/s3/client_test.go +++ b/pkg/s3/client_test.go @@ -102,7 +102,7 @@ func TestValidBucketNames(t *testing.T) { } for _, bt := range m { - got := IsValidBucket(bt.in) + got := IsValidBucketName(bt.in) if got != bt.want { t.Errorf("func(%q) = %v; want %v", bt.in, got, bt.want) }