s3/gcs: now all combinations and permutations work for URLs with buckets in different regions.

- <bucket>.s3.amazonaws.com with <bucket> in us-west-2 re-writes
   the URL to s3-us-west-2.amazonaws.com.
 - <bucket>.s3-us-west-2.amazonaws.com doesn't need and translation,
   call goes directly to s3-us-west-2.amazonaws.com
This commit is contained in:
Harshavardhana
2015-11-12 20:29:20 -08:00
parent bdb04d5dc8
commit 22051bb8af
7 changed files with 78 additions and 67 deletions
-9
View File
@@ -196,12 +196,3 @@ func url2Client(url string) (client.Client, *probe.Error) {
}
return client, nil
}
// bucketExists returns error
func bucketExists(URL string) *probe.Error {
_, _, err := url2Stat(URL)
if err != nil {
return err.Trace()
}
return nil
}
-5
View File
@@ -80,11 +80,6 @@ func checkCopySyntax(ctx *cli.Context) {
if url.Path == string(url.Separator) {
fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL))
}
if strings.Count(url.Path, "/") < 2 {
if err := bucketExists(tgtURL); err != nil {
fatalIf(err.Trace(), fmt.Sprintf("Unable to stat target ‘%s’.", tgtURL))
}
}
}
switch guessCopyURLType(srcURLs, tgtURL) {
case copyURLsTypeA: // File -> File.
+10 -9
View File
@@ -266,14 +266,8 @@ func (c *s3Client) Stat() (*client.Content, *probe.Error) {
// Currently only supported hosts with virtual style are Amazon S3 and Google Cloud Storage.
func isVirtualHostStyle(hostURL string) bool {
matchS3, _ := filepath.Match("*.s3*.amazonaws.com", hostURL)
if matchS3 {
return true
}
matchGoogle, _ := filepath.Match("*.storage.googleapis.com", hostURL)
if matchGoogle {
return true
}
return false
return matchS3 || matchGoogle
}
// url2BucketAndObject gives bucketName and objectName from URL path
@@ -284,8 +278,15 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) {
// For the time being this check is introduced for S3,
// if you have custom virtual styled hosts please. list them below
if c.virtualStyle {
hostSplits := strings.SplitN(c.hostURL.Host, ".", 2)
path = string(c.hostURL.Separator) + hostSplits[0] + c.hostURL.Path
var bucket string
hostIndex := strings.Index(c.hostURL.Host, "s3")
if hostIndex == -1 {
hostIndex = strings.Index(c.hostURL.Host, "storage.googleapis")
}
if hostIndex > 0 {
bucket = c.hostURL.Host[:hostIndex-1]
path = string(c.hostURL.Separator) + bucket + c.hostURL.Path
}
}
splits := strings.SplitN(path, string(c.hostURL.Separator), 3)
switch len(splits) {
+63 -39
View File
@@ -96,7 +96,6 @@ type ObjectMultipartStat struct {
var regions = map[string]string{
"s3-fips-us-gov-west-1.amazonaws.com": "us-gov-west-1",
"s3.amazonaws.com": "us-east-1",
"s3-external-1.amazonaws.com": "us-east-1",
"s3-us-west-1.amazonaws.com": "us-west-1",
"s3-us-west-2.amazonaws.com": "us-west-2",
"s3-eu-west-1.amazonaws.com": "eu-west-1",
@@ -179,9 +178,9 @@ type Config struct {
/// Internal options
// use SetUserAgent append to default, useful when minio-go is used with in your application
userAgent string
isUserAgentSet bool // allow user agent's to be set only once
isVirtualStyle bool // set when virtual hostnames are on
userAgent string
isUserAgentSet bool // allow user agent's to be set only once
isVirtualHostedStyle bool // set when virtual hostnames are on
}
// Global constants
@@ -208,10 +207,10 @@ type API struct {
apiCore
}
func isBucketVirtualStyle(host string) bool {
s3Virtual, _ := filepath.Match("*.s3*.amazonaws.com", host)
googleVirtual, _ := filepath.Match("*.storage.googleapis.com", host)
return s3Virtual || googleVirtual
func isVirtualHostedStyle(host string) bool {
isS3VirtualHost, _ := filepath.Match("*.s3*.amazonaws.com", host)
isGoogleVirtualHost, _ := filepath.Match("*.storage.googleapis.com", host)
return isS3VirtualHost || isGoogleVirtualHost
}
// New - instantiate minio client API with your input Config{}.
@@ -220,59 +219,84 @@ func New(config Config) (CloudStorageAPI, error) {
if err != nil {
return API{}, err
}
config.isVirtualStyle = isBucketVirtualStyle(u.Host)
config.SetUserAgent(LibraryName, LibraryVersion, runtime.GOOS, runtime.GOARCH)
config.isUserAgentSet = false // default
region, err := getBucketRegion(config)
if err != nil {
return API{}, err
config.isVirtualHostedStyle = isVirtualHostedStyle(u.Host)
// if not region is set, procure it from getBucketRegion if possible.
if config.Region == "" {
config = setBucketRegion(config)
}
config.Region = region
if config.Region == "google" {
config.Signature = SignatureV2 // Google cloud storage is signature V2
}
config.SetUserAgent(LibraryName, LibraryVersion, runtime.GOOS, runtime.GOARCH)
config.isUserAgentSet = false // default
return API{apiCore{&config}}, nil
}
// getBucketRegion returns the region
func getBucketRegion(config Config) (string, error) {
if config.Region != "" {
return config.Region, nil
}
// setBucketRegion fetches the region and constructs a proper endpoint
func setBucketRegion(config Config) Config {
u, err := url.Parse(config.Endpoint)
if err != nil {
return "", err
return config
}
if !isBucketVirtualStyle(u.Host) {
return getRegion(u.Host), nil
if !config.isVirtualHostedStyle {
config.Region = getRegion(u.Host)
return config
}
hostSplits := strings.SplitN(u.Host, ".", 2)
bucket := hostSplits[0]
host := hostSplits[1]
var bucket, host string
hostIndex := strings.Index(u.Host, "s3")
if hostIndex == -1 {
hostIndex = strings.Index(u.Host, "storage.googleapis.com")
}
if hostIndex > 0 {
host = u.Host[hostIndex:]
bucket = u.Host[:hostIndex-1]
}
genericGoogle, _ := filepath.Match("*.storage.googleapis.com", u.Host)
genericS3, _ := filepath.Match("*.s3.amazonaws.com", u.Host)
if !genericGoogle && !genericS3 {
return getRegion(host), nil
}
if genericGoogle {
// returning standard region for google for now, can be changed in future
// to query for region in case it is useful
return getRegion(host), nil
config.Region = getRegion(host)
return config
}
genericS3, _ := filepath.Match("*.s3.amazonaws.com", u.Host)
if !genericS3 {
config.Region = getRegion(host)
return config
}
// query aws s3 for the region for case of bucketName.s3.amazonaws.com
u.Host = host
config.Endpoint = u.String()
config.Region = getRegion(u.Host)
config.isVirtualStyle = false
s3api := API{apiCore{&config}}
return s3api.getBucketLocation(bucket)
tempConfig := Config{}
tempConfig.AccessKeyID = config.AccessKeyID
tempConfig.SecretAccessKey = config.SecretAccessKey
tempConfig.Endpoint = u.String()
tempConfig.Region = getRegion(u.Host)
tempConfig.isVirtualHostedStyle = false
s3api := API{apiCore{&tempConfig}}
region, err := s3api.getBucketLocation(bucket)
if err != nil {
config.Region = getRegion(host)
return config
}
if region == "" {
if genericS3 || genericGoogle {
region = "us-east-1"
}
}
config.Region = region
for k, v := range regions {
if region == v {
host = k
}
}
newURL := new(url.URL)
newURL.Host = bucket + "." + host
newURL.Scheme = u.Scheme
config.Endpoint = newURL.String()
return config
}
// PresignedPostPolicy return POST form data that can be used for object upload.
+1 -1
View File
@@ -157,7 +157,7 @@ func getURLEncodedPath(pathName string) string {
func (op *operation) getRequestURL(config Config) (url string) {
// parse URL for the combination of HTTPServer + HTTPPath
url = op.HTTPServer + separator
if !config.isVirtualStyle {
if !config.isVirtualHostedStyle {
url += path2Bucket(op.HTTPPath)
}
objectName := getURLEncodedPath(path2Object(op.HTTPPath))
+2 -2
View File
@@ -44,7 +44,7 @@ func (r *Request) PreSignV2() (string, error) {
}
epochExpires := d.Unix() + r.expires
var path string
if r.config.isVirtualStyle {
if r.config.isVirtualHostedStyle {
for k, v := range regions {
if v == r.config.Region {
path = "/" + strings.TrimSuffix(r.req.URL.Host, "."+k)
@@ -215,7 +215,7 @@ var resourceList = []string{
// [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
func (r *Request) writeCanonicalizedResource(buf *bytes.Buffer) error {
requestURL := r.req.URL
if r.config.isVirtualStyle {
if r.config.isVirtualHostedStyle {
for k, v := range regions {
if v == r.config.Region {
path := "/" + strings.TrimSuffix(requestURL.Host, "."+k)
+2 -2
View File
@@ -29,8 +29,8 @@
},
{
"path": "github.com/minio/minio-go",
"revision": "75080104fbebe0e0ccf9c97a332eba0b2a447831",
"revisionTime": "2015-11-12T14:56:32-08:00"
"revision": "11d470378cca55f1f2b67860f21a0d6ee3a16b60",
"revisionTime": "2015-11-12T20:28:50-08:00"
},
{
"path": "github.com/minio/minio-xl/pkg/probe",