From 741febd318e7e1811a7a54ca01603ad3da4a57fa Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 22 Jan 2015 12:37:57 -0800 Subject: [PATCH] Add minio cli, move 's3' stuff with 's3' prefix --- common.go | 40 ++++++++- configure.go | 31 ++++++- minio-api-options.go | 122 ++++++++++++++++++++++++++ minio-get-object.go | 58 ++++++++++++ pkg/minio/auth.go | 7 ++ pkg/test.go | 23 ----- api-options.go => s3-api-options.go | 4 +- errors.go => s3-errors.go | 0 fs-cp.go => s3-fs-cp.go | 0 fs-ls.go => s3-fs-ls.go | 0 fs-mkdir.go => s3-fs-mkdir.go | 0 fs-options.go => s3-fs-options.go | 0 get-object.go => s3-get-object.go | 0 list-buckets.go => s3-list-buckets.go | 0 list-objects.go => s3-list-objects.go | 0 put-object.go => s3-put-object.go | 0 16 files changed, 255 insertions(+), 30 deletions(-) create mode 100644 minio-api-options.go create mode 100644 minio-get-object.go create mode 100644 pkg/minio/auth.go delete mode 100644 pkg/test.go rename api-options.go => s3-api-options.go (98%) rename errors.go => s3-errors.go (100%) rename fs-cp.go => s3-fs-cp.go (100%) rename fs-ls.go => s3-fs-ls.go (100%) rename fs-mkdir.go => s3-fs-mkdir.go (100%) rename fs-options.go => s3-fs-options.go (100%) rename get-object.go => s3-get-object.go (100%) rename list-buckets.go => s3-list-buckets.go (100%) rename list-objects.go => s3-list-objects.go (100%) rename put-object.go => s3-put-object.go (100%) diff --git a/common.go b/common.go index 061a3a9b..13746d13 100644 --- a/common.go +++ b/common.go @@ -22,6 +22,7 @@ import ( "path" "github.com/codegangsta/cli" + "github.com/minio-io/mc/pkg/minio" "github.com/minio-io/mc/pkg/s3" ) @@ -36,6 +37,11 @@ var Options = []cli.Command{ Usage: "", Subcommands: subS3APIOptions, }, + { + Name: "minio", + Usage: "", + Subcommands: subMinioApiOptions, + }, } var subS3Options = []cli.Command{ @@ -53,15 +59,43 @@ var subS3APIOptions = []cli.Command{ Configure, } -func getAuthFilePath() string { +var subMinioApiOptions = []cli.Command{ + MinioGetObject, + MinioPutObject, + MinioListObjects, + MinioListBuckets, + MinioConfigure, +} + +func getAWSAuthFilePath() string { home := os.Getenv("HOME") - return path.Join(home, AUTH) + return path.Join(home, S3_AUTH) +} + +func getMinioAuthFilePath() string { + home := os.Getenv("HOME") + return path.Join(home, MINIO_AUTH) +} + +// TODO reply full json +func getMinioEnvironment() (hostname string, err error) { + var auth *minio.Auth + minioAuth, err := os.OpenFile(getMinioAuthFilePath(), os.O_RDWR, 0666) + defer minioAuth.Close() + var n int + minioAuthbytes := make([]byte, 256) + n, err = minioAuth.Read(minioAuthbytes) + err = json.Unmarshal(minioAuthbytes[:n], &auth) + if err != nil { + return "", err + } + return auth.Hostname, nil } func getAWSEnvironment() (auth *s3.Auth, err error) { var s3Auth *os.File var accessKey, secretKey string - s3Auth, err = os.OpenFile(getAuthFilePath(), os.O_RDWR, 0666) + s3Auth, err = os.OpenFile(getAWSAuthFilePath(), os.O_RDWR, 0666) defer s3Auth.Close() if err != nil { accessKey = os.Getenv("AWS_ACCESS_KEY_ID") diff --git a/configure.go b/configure.go index 1b22c4e9..e06a4645 100644 --- a/configure.go +++ b/configure.go @@ -7,6 +7,7 @@ import ( "path" "github.com/codegangsta/cli" + "github.com/minio-io/mc/pkg/minio" "github.com/minio-io/mc/pkg/s3" ) @@ -22,7 +23,33 @@ func parseConfigureInput(c *cli.Context) (accessKey, secretKey string, err error return accessKey, secretKey, nil } -func doConfigure(c *cli.Context) { +func doMinioConfigure(c *cli.Context) { + hostname := c.String("hostname") + if hostname == "" { + log.Fatal("Invalid hostname") + } + auth := &minio.Auth{"", "", hostname} + jAuth, err := json.Marshal(auth) + if err != nil { + log.Fatal(err) + } + + var minioFile *os.File + home := os.Getenv("HOME") + minioFile, err = os.OpenFile(path.Join(home, MINIO_AUTH), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + defer minioFile.Close() + if err != nil { + log.Fatal(err) + } + + _, err = minioFile.Write(jAuth) + if err != nil { + log.Fatal(err) + } + +} + +func doS3Configure(c *cli.Context) { var err error var jAuth []byte var accessKey, secretKey string @@ -39,7 +66,7 @@ func doConfigure(c *cli.Context) { var s3File *os.File home := os.Getenv("HOME") - s3File, err = os.OpenFile(path.Join(home, AUTH), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + s3File, err = os.OpenFile(path.Join(home, S3_AUTH), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) defer s3File.Close() if err != nil { log.Fatal(err) diff --git a/minio-api-options.go b/minio-api-options.go new file mode 100644 index 00000000..f77ae321 --- /dev/null +++ b/minio-api-options.go @@ -0,0 +1,122 @@ +/* + * Mini Object Storage, (C) 2014,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 main + +import ( + "github.com/codegangsta/cli" +) + +var MinioGetObject = cli.Command{ + Name: "get-object", + Usage: "", + Description: "Retrieves objects from Amazon S3.", + Action: minioGetObject, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "bucket", + Value: "", + Usage: "bucket name", + }, + cli.StringFlag{ + Name: "key", + Value: "", + Usage: "path to Object", + }, + }, +} + +var MinioPutObject = cli.Command{ + Name: "put-object", + Usage: "", + Description: "Adds an object to a bucket.", + Action: minioPutObject, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "bucket", + Value: "", + Usage: "bucket name", + }, + cli.StringFlag{ + Name: "key", + Value: "", + Usage: "Object name", + }, + cli.StringFlag{ + Name: "body", + Value: "", + Usage: "Object blob", + }, + }, +} + +var MinioListObjects = cli.Command{ + Name: "list-objects", + Usage: "", + Description: `Returns some or all (up to 1000) of the objects in a bucket. + You can use the request parameters as selection criteria to + return a subset of the objects in a bucket.`, + Action: minioListObjects, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "bucket", + Value: "", + Usage: "Bucket name", + }, + }, +} + +var MinioListBuckets = cli.Command{ + Name: "list-buckets", + Usage: "", + Description: `Returns a list of all buckets owned by the authenticated + sender of the request.`, + Action: minioListBuckets, +} + +var MinioConfigure = 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 + 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.`, + Action: doMinioConfigure, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "hostname", + Value: "127.0.0.1:8080", + Usage: "Minio object server", + }, + }, +} + +const ( + MINIO_AUTH = ".minioauth" +) + +func minioPutObject(c *cli.Context) { + //TODO +} + +func minioListBuckets(c *cli.Context) { + //TODO +} + +func minioListObjects(c *cli.Context) { + //TODO +} diff --git a/minio-get-object.go b/minio-get-object.go new file mode 100644 index 00000000..22b8fb6c --- /dev/null +++ b/minio-get-object.go @@ -0,0 +1,58 @@ +/* + * Mini Object Storage, (C) 2014,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 main + +import ( + "io" + "log" + "os" + + "github.com/codegangsta/cli" + "github.com/minio-io/mc/pkg/minio" +) + +func minioGetObject(c *cli.Context) { + var bucket, key, hostname string + var err error + var objectReader io.ReadCloser + var objectSize int64 + + hostname, err = getMinioEnvironment() + if err != nil { + log.Fatal(err) + } + + bucket = c.String("bucket") + key = c.String("key") + if bucket == "" { + log.Fatal(bucketNameErr) + } + if key == "" { + log.Fatal(objectNameErr) + } + + minio := minio.NewMinioClient(hostname) + objectReader, objectSize, err = minio.Get(bucket, key) + if err != nil { + log.Fatal(err) + } + + _, err = io.CopyN(os.Stdout, objectReader, objectSize) + if err != nil { + log.Fatal(err) + } +} diff --git a/pkg/minio/auth.go b/pkg/minio/auth.go new file mode 100644 index 00000000..abf85f0a --- /dev/null +++ b/pkg/minio/auth.go @@ -0,0 +1,7 @@ +package minio + +type Auth struct { + AccessKey string + SecretAccessKey string + Hostname string +} diff --git a/pkg/test.go b/pkg/test.go deleted file mode 100644 index 30031c87..00000000 --- a/pkg/test.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "io" - "log" - "os" - - "github.com/minio-io/mc/pkg/minio" -) - -func main() { - newminio := minio.NewMinioClient("127.0.0.1:8080") - objectReader, objectSize, err := newminio.Get("bucketname", "test") - if err != nil { - log.Fatal(err) - } - - _, err = io.CopyN(os.Stdout, objectReader, objectSize) - if err != nil { - log.Fatal(err) - } - -} diff --git a/api-options.go b/s3-api-options.go similarity index 98% rename from api-options.go rename to s3-api-options.go index e038509d..bd983a9d 100644 --- a/api-options.go +++ b/s3-api-options.go @@ -95,7 +95,7 @@ var Configure = cli.Command{ 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.`, - Action: doConfigure, + Action: doS3Configure, Flags: []cli.Flag{ cli.StringFlag{ Name: "accesskey", @@ -111,5 +111,5 @@ var Configure = cli.Command{ } const ( - AUTH = ".auth" + S3_AUTH = ".auth" ) diff --git a/errors.go b/s3-errors.go similarity index 100% rename from errors.go rename to s3-errors.go diff --git a/fs-cp.go b/s3-fs-cp.go similarity index 100% rename from fs-cp.go rename to s3-fs-cp.go diff --git a/fs-ls.go b/s3-fs-ls.go similarity index 100% rename from fs-ls.go rename to s3-fs-ls.go diff --git a/fs-mkdir.go b/s3-fs-mkdir.go similarity index 100% rename from fs-mkdir.go rename to s3-fs-mkdir.go diff --git a/fs-options.go b/s3-fs-options.go similarity index 100% rename from fs-options.go rename to s3-fs-options.go diff --git a/get-object.go b/s3-get-object.go similarity index 100% rename from get-object.go rename to s3-get-object.go diff --git a/list-buckets.go b/s3-list-buckets.go similarity index 100% rename from list-buckets.go rename to s3-list-buckets.go diff --git a/list-objects.go b/s3-list-objects.go similarity index 100% rename from list-objects.go rename to s3-list-objects.go diff --git a/put-object.go b/s3-put-object.go similarity index 100% rename from put-object.go rename to s3-put-object.go