Skip to content

Commit

Permalink
bucket url generation improvements
Browse files Browse the repository at this point in the history
Fixes #68

bucket url generation improvements
  • Loading branch information
rlmcpherson committed Dec 1, 2015
1 parent c70ac93 commit 14107eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
23 changes: 12 additions & 11 deletions s3gof3r.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,22 @@ func (b *Bucket) PutWriter(path string, h http.Header, c *Config) (w io.WriteClo
// Note: Urls containing some special characters will fail due to net/http bug.
// See https://code.google.com/p/go/issues/detail?id=5684
func (b *Bucket) url(bPath string, c *Config) (*url.URL, error) {
u, err := url.Parse(bPath)
if err != nil {
return nil, err
}
u.Scheme = c.Scheme
// handling for bucket names containing periods

// handling for bucket names containing periods / explicit PathStyle addressing
// http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html for details
if strings.Contains(b.Name, ".") || c.PathStyle {
u.Host = b.S3.Domain
u.Path = path.Clean(fmt.Sprintf("/%s/%s", b.Name, u.Path))
return &url.URL{
Host: b.S3.Domain,
Scheme: c.Scheme,
Path: path.Clean(fmt.Sprintf("/%s/%s", b.Name, bPath)),
}, nil
} else {
u.Host = fmt.Sprintf("%s.%s", b.Name, b.S3.Domain)
u.Path = path.Clean(fmt.Sprintf("/%s", u.Path))
return &url.URL{
Scheme: c.Scheme,
Path: bPath,
Host: fmt.Sprintf("%s.%s", b.Name, b.S3.Domain),
}, nil
}
return u, nil
}

func (b *Bucket) conf() *Config {
Expand Down
28 changes: 28 additions & 0 deletions s3gof3r_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,34 @@ func TestRegion(t *testing.T) {
}
}

func TestBucketURL(t *testing.T) {
var urlTests = []struct {
bucket string
path string
config *Config
url string
}{
{"bucket1", "path", DefaultConfig, "https://bucket1.s3.amazonaws.com/path"},
{"bucket1", "#path", DefaultConfig, `https://bucket1.s3.amazonaws.com/%23path`},
{"bucket.2", "path", DefaultConfig, "https://s3.amazonaws.com/bucket.2/path"},
{"bucket.2", "#path", DefaultConfig, `https://s3.amazonaws.com/bucket.2/%23path`},
}

for _, tt := range urlTests {
s3 := New("", Keys{})
b := s3.Bucket(tt.bucket)
u, err := b.url(tt.path, tt.config)
if err != nil {
t.Error(err)
}
if u.String() != tt.url {
t.Errorf("got '%s', expected '%s'", u.String(), tt.url)
}

}

}

// reduce parallelism and part size to benchmark
// memory pool reuse
func benchConfig() *Config {
Expand Down

0 comments on commit 14107eb

Please # to comment.