Merge pull request #159 from harshavardhana/pr_out_cleanup_janitor_duty

This commit is contained in:
Harshavardhana
2015-03-22 20:38:48 -07:00
7 changed files with 24 additions and 29 deletions
+1 -1
View File
@@ -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())
}
+4 -2
View File
@@ -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
}
+6 -20
View File
@@ -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 {
View File
+1 -1
View File
@@ -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)
+11 -4
View File
@@ -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
}
+1 -1
View File
@@ -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)
}