From c0bdcad429c101b476f6454ed921351de263ffc7 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 24 Feb 2015 13:54:22 -0800 Subject: [PATCH] Move Hostname to Endpoint, remove md5sum calculation for now. --- common.go | 2 +- configure.go | 18 +++++++++++------- pkg/s3/auth.go | 14 +++++++------- pkg/s3/client.go | 3 ++- s3-errors.go | 1 + s3-fs-cp.go | 47 +++++++++-------------------------------------- s3-fs-options.go | 7 ++++++- 7 files changed, 37 insertions(+), 55 deletions(-) diff --git a/common.go b/common.go index 670337d4..a85d39ea 100644 --- a/common.go +++ b/common.go @@ -59,7 +59,7 @@ func getAWSEnvironment() (auth *s3.Auth, err error) { auth = &s3.Auth{ AccessKey: accessKey, SecretAccessKey: secretKey, - Hostname: "s3.amazonaws.com", + Endpoint: "s3.amazonaws.com", } } else { var n int diff --git a/configure.go b/configure.go index 1b22c4e9..638c8341 100644 --- a/configure.go +++ b/configure.go @@ -10,28 +10,32 @@ import ( "github.com/minio-io/mc/pkg/s3" ) -func parseConfigureInput(c *cli.Context) (accessKey, secretKey string, err error) { +func parseConfigureInput(c *cli.Context) (accessKey, secretKey, endpoint string, err error) { accessKey = c.String("accesskey") secretKey = c.String("secretkey") + endpoint = c.String("endpoint") if accessKey == "" { - return "", "", configAccessErr + return "", "", "", configAccessErr } if secretKey == "" { - return "", "", configSecretErr + return "", "", "", configSecretErr } - return accessKey, secretKey, nil + if endpoint == "" { + return "", "", "", configEndpointErr + } + return accessKey, secretKey, endpoint, nil } func doConfigure(c *cli.Context) { var err error var jAuth []byte - var accessKey, secretKey string - accessKey, secretKey, err = parseConfigureInput(c) + var accessKey, secretKey, endpoint string + accessKey, secretKey, endpoint, err = parseConfigureInput(c) if err != nil { log.Fatal(err) } - auth := s3.NewAuth(accessKey, secretKey, "s3.amazonaws.com") + auth := s3.NewAuth(accessKey, secretKey, endpoint) jAuth, err = json.Marshal(auth) if err != nil { log.Fatal(err) diff --git a/pkg/s3/auth.go b/pkg/s3/auth.go index cf322af4..7474c88a 100644 --- a/pkg/s3/auth.go +++ b/pkg/s3/auth.go @@ -41,7 +41,7 @@ type Auth struct { SecretAccessKey string // If empty, the standard US region of "s3.amazonaws.com" is used. - Hostname string + Endpoint string // Used for SSL transport layer CertPEM string @@ -57,11 +57,11 @@ const standardUSRegionAWS = "s3.amazonaws.com" func (a *Auth) hostname() string { // Prefix with https for Amazon hostnames - if a.Hostname != "" { - if strings.HasSuffix(a.Hostname, "amazonaws.com") { - return "https://" + a.Hostname + if a.Endpoint != "" { + if strings.HasSuffix(a.Endpoint, "amazonaws.com") { + return "https://" + a.Endpoint } else { - return "http://" + a.Hostname + return "http://" + a.Endpoint } } return "https://" + standardUSRegionAWS @@ -219,7 +219,7 @@ var subResList = []string{"acl", "lifecycle", "location", "logging", "notificati // + // [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"]; func (a *Auth) writeCanonicalizedResource(buf *bytes.Buffer, req *http.Request) { - bucket := a.bucketFromHostname(req) + bucket := a.bucketFromEndpoint(req) if bucket != "" { buf.WriteByte('/') buf.WriteString(bucket) @@ -251,7 +251,7 @@ func hasDotSuffix(s string, suffix string) bool { return len(s) >= len(suffix)+1 && strings.HasSuffix(s, suffix) && s[len(s)-len(suffix)-1] == '.' } -func (a *Auth) bucketFromHostname(req *http.Request) string { +func (a *Auth) bucketFromEndpoint(req *http.Request) string { host := req.Host if host == "" { host = req.URL.Host diff --git a/pkg/s3/client.go b/pkg/s3/client.go index d856e766..a5fbc4ed 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -377,7 +377,8 @@ func NewAuth(accessKey, secretKey, hostname string) (auth *Auth) { auth = &Auth{ AccessKey: accessKey, SecretAccessKey: secretKey, - Hostname: hostname} + Endpoint: hostname, + } return } diff --git a/s3-errors.go b/s3-errors.go index f4654bb7..a5053703 100644 --- a/s3-errors.go +++ b/s3-errors.go @@ -31,6 +31,7 @@ var fsKeyErr = errors.New("Key is needed to get the file") // configure var configAccessErr = errors.New("accesskey is mandatory") var configSecretErr = errors.New("secretkey is mandatory") +var configEndpointErr = errors.New("endpoint is missing") // common var missingAccessSecretErr = errors.New("You can configure your credentials by running `mc configure`") diff --git a/s3-fs-cp.go b/s3-fs-cp.go index 496c6440..d23aa9dd 100644 --- a/s3-fs-cp.go +++ b/s3-fs-cp.go @@ -17,9 +17,6 @@ package main import ( - "bytes" - "crypto/md5" - "hash" "io" "log" "os" @@ -35,32 +32,6 @@ import ( // - // - -func getPutMetadata(reader io.Reader) (md5hash hash.Hash, bodyBuf io.Reader, size int64, err error) { - md5hash = md5.New() - var length int - var bodyBuffer bytes.Buffer - - for err == nil { - byteBuffer := make([]byte, 1024*1024) - length, err = reader.Read(byteBuffer) - // While hash.Write() wouldn't mind a Nil byteBuffer - // It is necessary for us to verify this and break - if length == 0 { - break - } - byteBuffer = byteBuffer[0:length] - _, err = bodyBuffer.Write(byteBuffer) - if err != nil { - break - } - md5hash.Write(byteBuffer) - } - if err != io.EOF { - return nil, nil, 0, err - } - return md5hash, &bodyBuffer, int64(bodyBuffer.Len()), nil -} - func parseCpOptions(c *cli.Context) (fsoptions fsOptions, err error) { switch len(c.Args()) { case 1: @@ -121,21 +92,21 @@ func doFsCopy(c *cli.Context) { } if fsoptions.isput { + stat, err := os.Stat(fsoptions.body) + if os.IsNotExist(err) { + log.Fatal(err) + } + if stat.IsDir() { + log.Fatal("Is a directory") + } + size := stat.Size() bodyFile, err = os.Open(fsoptions.body) defer bodyFile.Close() if err != nil { log.Fatal(err) } - var bodyBuffer io.Reader - var size int64 - var md5hash hash.Hash - md5hash, bodyBuffer, size, err = getPutMetadata(bodyFile) - if err != nil { - log.Fatal(err) - } - - err = s3c.Put(fsoptions.bucket, fsoptions.key, md5hash, size, bodyBuffer) + err = s3c.Put(fsoptions.bucket, fsoptions.key, nil, size, bodyFile) if err != nil { log.Fatal(err) } diff --git a/s3-fs-options.go b/s3-fs-options.go index d90f26f9..568cabd7 100644 --- a/s3-fs-options.go +++ b/s3-fs-options.go @@ -52,7 +52,7 @@ var Configure = cli.Command{ Name: "configure", Usage: "", Description: `Configure minio client configuration data. If your config - file does not exist (the default location is ~/.s3auth), it will be + file does not exist (the default location is ~/.auth), it will be automatically created for you. Note that the configure command only writes values to the config file. It does not use any configuration values from the environment variables.`, @@ -68,6 +68,11 @@ var Configure = cli.Command{ Value: "", Usage: "AWS secret key id", }, + cli.StringFlag{ + Name: "endpoint", + Value: "s3.amazonaws.com", + Usage: "S3 Endpoint URL default is 's3.amazonaws.com'", + }, }, }