Merge pull request #1507 from harshavardhana/fix-region
region: Resolve region properly, if fails return the error.
This commit is contained in:
+1
-1
@@ -48,7 +48,7 @@ func (m mirrorURLs) isEmpty() bool {
|
||||
|
||||
// checkMirrorSyntax(URLs []string)
|
||||
func checkMirrorSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) < 2 {
|
||||
if len(ctx.Args()) != 2 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "mirror", 1) // last argument is exit code.
|
||||
}
|
||||
|
||||
|
||||
@@ -84,11 +84,21 @@ func pipe(targetURL string) *probe.Error {
|
||||
return err.Trace(targetURL)
|
||||
}
|
||||
|
||||
// check pipe input arguments.
|
||||
func checkPipeSyntax(ctx *cli.Context) {
|
||||
if len(ctx.Args()) > 1 {
|
||||
cli.ShowCommandHelpAndExit(ctx, "pipe", 1) // last argument is exit code.
|
||||
}
|
||||
}
|
||||
|
||||
// mainPipe is the main entry point for pipe command.
|
||||
func mainPipe(ctx *cli.Context) {
|
||||
// Set global flags from context.
|
||||
setGlobalsFromContext(ctx)
|
||||
|
||||
// validate pipe input arguments.
|
||||
checkPipeSyntax(ctx)
|
||||
|
||||
if len(ctx.Args()) == 0 {
|
||||
err := pipe("")
|
||||
fatalIf(err.Trace("stdout"), "Unable to write to one or more targets.")
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ func (c *s3Client) Put(data io.ReadSeeker, size int64) *probe.Error {
|
||||
// invidual parts are properly verified fully in transit and also upon completion
|
||||
// of the multipart request.
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
err := c.api.PutObject(bucket, object, "application/octet-stream", data, size)
|
||||
err := c.api.PutObject(bucket, object, data, size, "application/octet-stream")
|
||||
if err != nil {
|
||||
errResponse := minio.ToErrorResponse(err)
|
||||
if errResponse != nil {
|
||||
|
||||
+22
-20
@@ -18,6 +18,8 @@ List of supported cloud storage providers.
|
||||
|
||||
## Install
|
||||
|
||||
If you do not have a working Golang environment, please follow [Install Golang](./INSTALLGO.md).
|
||||
|
||||
```sh
|
||||
$ go get github.com/minio/minio-go
|
||||
```
|
||||
@@ -39,14 +41,14 @@ import (
|
||||
|
||||
func main() {
|
||||
config := minio.Config{
|
||||
Endpoint: "https://s3.amazonaws.com",
|
||||
AccessKeyID: "YOUR-ACCESS-KEY-HERE",
|
||||
SecretAccessKey: "YOUR-PASSWORD-HERE",
|
||||
Endpoint: "https://s3.amazonaws.com",
|
||||
}
|
||||
|
||||
// Default is Signature Version 4. To enable Signature Version 2 do the following.
|
||||
// config.Signature = minio.SignatureV2
|
||||
|
||||
|
||||
s3Client, err := minio.New(config)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
@@ -62,27 +64,27 @@ func main() {
|
||||
|
||||
## Documentation
|
||||
|
||||
### Bucket Level
|
||||
* [MakeBucket(bucket, acl) error](examples/s3/makebucket.go)
|
||||
* [BucketExists(bucket) error](examples/s3/bucketexists.go)
|
||||
* [RemoveBucket(bucket) error](examples/s3/removebucket.go)
|
||||
* [GetBucketACL(bucket) (BucketACL, error)](examples/s3/getbucketacl.go)
|
||||
* [SetBucketACL(bucket, BucketACL) error)](examples/s3/setbucketacl.go)
|
||||
### Bucket Operations.
|
||||
* [MakeBucket(bucketName, BucketACL) error](examples/s3/makebucket.go)
|
||||
* [BucketExists(bucketName) error](examples/s3/bucketexists.go)
|
||||
* [RemoveBucket(bucketName) error](examples/s3/removebucket.go)
|
||||
* [GetBucketACL(bucketName) (BucketACL, error)](examples/s3/getbucketacl.go)
|
||||
* [SetBucketACL(bucketName, BucketACL) error)](examples/s3/setbucketacl.go)
|
||||
* [ListBuckets() <-chan BucketStat](examples/s3/listbuckets.go)
|
||||
* [ListObjects(bucket, prefix, recursive) <-chan ObjectStat](examples/s3/listobjects.go)
|
||||
* [ListIncompleteUploads(bucket, prefix, recursive) <-chan ObjectMultipartStat](examples/s3/listincompleteuploads.go)
|
||||
* [ListObjects(bucketName, prefix, recursive) <-chan ObjectStat](examples/s3/listobjects.go)
|
||||
* [ListIncompleteUploads(bucketName, prefix, recursive) <-chan ObjectMultipartStat](examples/s3/listincompleteuploads.go)
|
||||
|
||||
### Object Level
|
||||
* [PutObject(bucket, object, contentType, io.ReadSeeker) error](examples/s3/putobject.go)
|
||||
* [GetObject(bucket, object) (io.ReadSeeker, error)](examples/s3/getobject.go)
|
||||
* [GetPartialObject(bucket, object, offset, length) (io.ReadSeeker, error)](examples/s3/getpartialobject.go)
|
||||
* [StatObject(bucket, object) (ObjectStat, error)](examples/s3/statobject.go)
|
||||
* [RemoveObject(bucket, object) error](examples/s3/removeobject.go)
|
||||
* [RemoveIncompleteUpload(bucket, object) <-chan error](examples/s3/removeincompleteupload.go)
|
||||
### Object Operations.
|
||||
* [PutObject(bucketName, objectName, contentType, io.ReadSeeker) error](examples/s3/putobject.go)
|
||||
* [GetObject(bucketName, objectName) (io.ReadSeeker, error)](examples/s3/getobject.go)
|
||||
* [GetPartialObject(bucketName, objectName, offset, length) (io.ReadSeeker, error)](examples/s3/getpartialobject.go)
|
||||
* [StatObject(bucketName, objectName) (ObjectStat, error)](examples/s3/statobject.go)
|
||||
* [RemoveObject(bucketName, objectName) error](examples/s3/removeobject.go)
|
||||
* [RemoveIncompleteUpload(bucketName, objectName) <-chan error](examples/s3/removeincompleteupload.go)
|
||||
|
||||
### Presigned Bucket/Object Level
|
||||
* [PresignedGetObject(bucket, object, time.Duration) (string, error)](examples/s3/presignedgetobject.go)
|
||||
* [PresignedPutObject(bucket, object, time.Duration) (string, error)](examples/s3/presignedputobject.go)
|
||||
### Presigned Operations.
|
||||
* [PresignedGetObject(bucketName, objectName, time.Duration) (string, error)](examples/s3/presignedgetobject.go)
|
||||
* [PresignedPutObject(bucketName, objectName, time.Duration) (string, error)](examples/s3/presignedputobject.go)
|
||||
* [PresignedPostPolicy(NewPostPolicy()) (map[string]string, error)](examples/s3/presignedpostpolicy.go)
|
||||
|
||||
### API Reference
|
||||
|
||||
+11
-10
@@ -146,15 +146,15 @@ func (c *Config) isAnonymous() bool {
|
||||
|
||||
// setBucketRegion fetches the region and updates config,
|
||||
// additionally it also constructs a proper endpoint based on that region.
|
||||
func (c *Config) setBucketRegion() {
|
||||
func (c *Config) setBucketRegion() error {
|
||||
u, err := url.Parse(c.Endpoint)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.isVirtualHostedStyle {
|
||||
c.Region = getRegion(u.Host)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
var bucket, host string
|
||||
@@ -172,12 +172,12 @@ func (c *Config) setBucketRegion() {
|
||||
// returning standard region for google for now, can be changed in future
|
||||
// to query for region in case it is useful
|
||||
c.Region = getRegion(host)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
genericS3, _ := filepath.Match("*.s3.amazonaws.com", u.Host)
|
||||
if !genericS3 {
|
||||
c.Region = getRegion(host)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// query aws s3 for the region for case of bucketName.s3.amazonaws.com
|
||||
@@ -191,8 +191,7 @@ func (c *Config) setBucketRegion() {
|
||||
s3API := API{s3API{&tempConfig}}
|
||||
region, err := s3API.getBucketLocation(bucket)
|
||||
if err != nil {
|
||||
c.Region = getRegion(host)
|
||||
return
|
||||
return err
|
||||
}
|
||||
// if region returned from getBucketLocation is null
|
||||
// and if genericS3 is enabled - set back to 'us-east-1'.
|
||||
@@ -203,7 +202,7 @@ func (c *Config) setBucketRegion() {
|
||||
}
|
||||
c.Region = region
|
||||
c.setEndpoint(region, bucket, u.Scheme)
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// setEndpoint - construct final endpoint based on region, bucket and scheme
|
||||
@@ -255,7 +254,9 @@ func New(config Config) (CloudStorageAPI, error) {
|
||||
config.isVirtualHostedStyle = isVirtualHostedStyle(u.Host)
|
||||
// if not region is set, procure it from getBucketRegion if possible.
|
||||
if config.Region == "" {
|
||||
config.setBucketRegion()
|
||||
if err := config.setBucketRegion(); err != nil {
|
||||
return API{}, err
|
||||
}
|
||||
}
|
||||
/// Google cloud storage should be set to signature V2, force it if not.
|
||||
if config.Region == "google" && config.Signature != SignatureV2 {
|
||||
@@ -584,7 +585,7 @@ func (a API) continueObjectUpload(bucket, object, uploadID string, size int64, d
|
||||
//
|
||||
// For un-authenticated requests S3 doesn't allow multipart upload, so we fall back to single
|
||||
// PUT operation.
|
||||
func (a API) PutObject(bucket, object, contentType string, data io.ReadSeeker, size int64) error {
|
||||
func (a API) PutObject(bucket, object string, data io.ReadSeeker, size int64, contentType string) error {
|
||||
if err := invalidBucketError(bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ func TestObjectOperations(t *testing.T) {
|
||||
t.Fatal("Error")
|
||||
}
|
||||
data := []byte("Hello, World")
|
||||
err = a.PutObject("bucket", "object", "", bytes.NewReader(data), int64(len(data)))
|
||||
err = a.PutObject("bucket", "object", bytes.NewReader(data), int64(len(data)), "")
|
||||
if err != nil {
|
||||
t.Fatal("Error")
|
||||
}
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ type CloudStorageAPI interface {
|
||||
// Object Read/Write/Stat operations
|
||||
GetObject(bucket, object string) (io.ReadSeeker, error)
|
||||
GetPartialObject(bucket, object string, offset, length int64) (io.ReadSeeker, error)
|
||||
PutObject(bucket, object, contentType string, data io.ReadSeeker, size int64) error
|
||||
PutObject(bucket, object string, data io.ReadSeeker, size int64, contentType string) error
|
||||
StatObject(bucket, object string) (ObjectStat, error)
|
||||
RemoveObject(bucket, object string) error
|
||||
RemoveIncompleteUpload(bucket, object string) <-chan error
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2015 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package minio
|
||||
|
||||
import "net/http"
|
||||
|
||||
// TODO - handle this automatically by re-writing the request as virtual style.
|
||||
//
|
||||
// For path style requests on buckets with wrong endpoint s3 returns back a
|
||||
// generic error. Following block of code tries to make this meaningful for
|
||||
// the user by fetching the proper endpoint. Additionally also sets AmzBucketRegion.
|
||||
func (a s3API) handleStatusMovedPermanently(resp *http.Response, bucket, object string) ErrorResponse {
|
||||
errorResponse := ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse.Resource = separator + bucket
|
||||
if object != "" {
|
||||
errorResponse.Resource = separator + bucket + separator + object
|
||||
}
|
||||
var endPoint string
|
||||
if errorResponse.AmzBucketRegion != "" {
|
||||
region := errorResponse.AmzBucketRegion
|
||||
endPoint = getEndpoint(region)
|
||||
} else {
|
||||
region, err := a.getBucketLocation(bucket)
|
||||
if err != nil {
|
||||
return *ToErrorResponse(err)
|
||||
}
|
||||
endPoint = getEndpoint(region)
|
||||
}
|
||||
msg := "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint."
|
||||
errorResponse.Message = msg
|
||||
return errorResponse
|
||||
}
|
||||
+12
-80
@@ -38,6 +38,10 @@ type s3API struct {
|
||||
}
|
||||
|
||||
// closeResp close non nil response with any response Body.
|
||||
// convenient wrapper to drain any remaining data on response body.
|
||||
//
|
||||
// Subsequently this allows golang http RoundTripper
|
||||
// to re-use the same connection for future requests.
|
||||
func closeResp(resp *http.Response) {
|
||||
// Callers should close resp.Body when done reading from it.
|
||||
// If resp.Body is not closed, the Client's underlying RoundTripper
|
||||
@@ -172,17 +176,7 @@ func (a s3API) putBucketACL(bucket, acl string) error {
|
||||
if resp != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode == http.StatusMovedPermanently {
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse := ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
return errorResponse
|
||||
return a.handleStatusMovedPermanently(resp, bucket, "")
|
||||
}
|
||||
return BodyToErrorResponse(resp.Body)
|
||||
}
|
||||
@@ -218,16 +212,7 @@ func (a s3API) getBucketACL(bucket string) (accessControlPolicy, error) {
|
||||
if resp != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode == http.StatusMovedPermanently {
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse := ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse := a.handleStatusMovedPermanently(resp, bucket, "")
|
||||
return accessControlPolicy{}, errorResponse
|
||||
}
|
||||
return accessControlPolicy{}, BodyToErrorResponse(resp.Body)
|
||||
@@ -354,16 +339,7 @@ func (a s3API) listObjects(bucket, marker, prefix, delimiter string, maxkeys int
|
||||
if resp != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode == http.StatusMovedPermanently {
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse := ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse := a.handleStatusMovedPermanently(resp, bucket, "")
|
||||
return listBucketResult{}, errorResponse
|
||||
}
|
||||
return listBucketResult{}, BodyToErrorResponse(resp.Body)
|
||||
@@ -408,14 +384,7 @@ func (a s3API) headBucket(bucket string) error {
|
||||
var errorResponse ErrorResponse
|
||||
switch resp.StatusCode {
|
||||
case http.StatusMovedPermanently:
|
||||
errorResponse = ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + getEndpoint(resp.Header.Get("x-amz-bucket-region")) + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse = a.handleStatusMovedPermanently(resp, bucket, "")
|
||||
case http.StatusNotFound:
|
||||
errorResponse = ErrorResponse{
|
||||
Code: "NoSuchBucket",
|
||||
@@ -483,16 +452,7 @@ func (a s3API) deleteBucket(bucket string) error {
|
||||
var errorResponse ErrorResponse
|
||||
switch resp.StatusCode {
|
||||
case http.StatusMovedPermanently:
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse = ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse = a.handleStatusMovedPermanently(resp, bucket, "")
|
||||
case http.StatusNotFound:
|
||||
errorResponse = ErrorResponse{
|
||||
Code: "NoSuchBucket",
|
||||
@@ -576,18 +536,8 @@ func (a s3API) putObject(bucket, object string, putObjMetadata putObjectMetadata
|
||||
}
|
||||
if resp != nil {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// handle 301 sepcifically in case of wrong regions during path style.
|
||||
if resp.StatusCode == http.StatusMovedPermanently {
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse := ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse := a.handleStatusMovedPermanently(resp, bucket, object)
|
||||
return ObjectStat{}, errorResponse
|
||||
}
|
||||
return ObjectStat{}, BodyToErrorResponse(resp.Body)
|
||||
@@ -743,16 +693,7 @@ func (a s3API) getObject(bucket, object string, offset, length int64) (io.ReadCl
|
||||
case http.StatusPartialContent:
|
||||
// handle 301 sepcifically in case of wrong regions during path style.
|
||||
case http.StatusMovedPermanently:
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse := ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse := a.handleStatusMovedPermanently(resp, bucket, object)
|
||||
return nil, ObjectStat{}, errorResponse
|
||||
default:
|
||||
return nil, ObjectStat{}, BodyToErrorResponse(resp.Body)
|
||||
@@ -849,16 +790,7 @@ func (a s3API) headObject(bucket, object string) (ObjectStat, error) {
|
||||
var errorResponse ErrorResponse
|
||||
switch resp.StatusCode {
|
||||
case http.StatusMovedPermanently:
|
||||
region, _ := a.getBucketLocation(bucket)
|
||||
endPoint := getEndpoint(region)
|
||||
errorResponse = ErrorResponse{
|
||||
Code: "PermanentRedirect",
|
||||
Message: "The bucket you are attempting to access must be addressed using the specified endpoint https://" + endPoint + ". Send all future requests to this endpoint.",
|
||||
Resource: separator + bucket,
|
||||
RequestID: resp.Header.Get("x-amz-request-id"),
|
||||
HostID: resp.Header.Get("x-amz-id-2"),
|
||||
AmzBucketRegion: resp.Header.Get("x-amz-bucket-region"),
|
||||
}
|
||||
errorResponse = a.handleStatusMovedPermanently(resp, bucket, object)
|
||||
case http.StatusNotFound:
|
||||
errorResponse = ErrorResponse{
|
||||
Code: "NoSuchKey",
|
||||
|
||||
Vendored
+2
-2
@@ -29,8 +29,8 @@
|
||||
},
|
||||
{
|
||||
"path": "github.com/minio/minio-go",
|
||||
"revision": "c6c4e52d680f679214985856e7bc08c3d1ae8a6b",
|
||||
"revisionTime": "2015-11-28T22:10:20-08:00"
|
||||
"revision": "aa6adeaec180d4166112fec7ed16f3c7e189bd32",
|
||||
"revisionTime": "2015-11-30T12:16:55-08:00"
|
||||
},
|
||||
{
|
||||
"path": "github.com/minio/minio-xl/pkg/atomic",
|
||||
|
||||
Reference in New Issue
Block a user