Add get-object, put-object in accordance with aws cli
This commit is contained in:
@@ -7,7 +7,7 @@ getdeps: checkdeps
|
||||
@go get github.com/tools/godep && echo "Installed godep"
|
||||
@go get golang.org/x/tools/cmd/cover && echo "Installed cover"
|
||||
|
||||
install:
|
||||
install: s3
|
||||
@godep go install github.com/minio-io/mc && echo "Installed mc"
|
||||
|
||||
s3:
|
||||
|
||||
+29
-32
@@ -17,11 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/minio-io/mc/pkg/s3"
|
||||
)
|
||||
|
||||
var GetObject = cli.Command{
|
||||
@@ -29,6 +25,18 @@ var GetObject = cli.Command{
|
||||
Usage: "",
|
||||
Description: "",
|
||||
Action: doGetObject,
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "bucket",
|
||||
Value: "",
|
||||
Usage: "bucket name",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "key",
|
||||
Value: "",
|
||||
Usage: "path to Object",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var PutObject = cli.Command{
|
||||
@@ -36,6 +44,23 @@ var PutObject = cli.Command{
|
||||
Usage: "",
|
||||
Description: "",
|
||||
Action: doPutObject,
|
||||
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 blobx",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var ListObjects = cli.Command{
|
||||
@@ -59,34 +84,6 @@ var Configure = cli.Command{
|
||||
Action: doConfigure,
|
||||
}
|
||||
|
||||
func doGetObject(c *cli.Context) {
|
||||
var bucket, key string
|
||||
accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
|
||||
secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
|
||||
if accessKey == "" && secretKey == "" {
|
||||
errstr := `You can configure your credentials by running "mc configure"`
|
||||
log.Fatal(errstr)
|
||||
}
|
||||
if accessKey == "" {
|
||||
errstr := `Partial credentials found in the env, missing : AWS_ACCESS_KEY_ID`
|
||||
log.Fatal(errstr)
|
||||
}
|
||||
|
||||
if secretKey == "" {
|
||||
errstr := `Partial credentials found in the env, missing : AWS_SECRET_ACCESS_KEY`
|
||||
log.Fatal(errstr)
|
||||
}
|
||||
|
||||
s3c := s3.NewS3Client(accessKey, secretKey)
|
||||
_, _, err := s3c.Get(bucket, key)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func doPutObject(c *cli.Context) {
|
||||
}
|
||||
|
||||
func doListObject(c *cli.Context) {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
)
|
||||
@@ -32,22 +32,22 @@ var Options = []cli.Command{
|
||||
Configure,
|
||||
}
|
||||
|
||||
func parseInput(c *cli.Context) string {
|
||||
var commandName string
|
||||
switch len(c.Args()) {
|
||||
case 1:
|
||||
commandName = c.Args()[0]
|
||||
default:
|
||||
log.Fatal("command name must not be blank\n")
|
||||
func getAWSEnvironment() (accessKey, secretKey string, err error) {
|
||||
accessKey = os.Getenv("AWS_ACCESS_KEY_ID")
|
||||
secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
|
||||
if accessKey == "" && secretKey == "" {
|
||||
errstr := `You can configure your credentials by running "mc configure"`
|
||||
return "", "", errors.New(errstr)
|
||||
}
|
||||
if accessKey == "" {
|
||||
errstr := `Partial credentials found in the env, missing : AWS_ACCESS_KEY_ID`
|
||||
return "", "", errors.New(errstr)
|
||||
}
|
||||
|
||||
var inputOptions []string
|
||||
if c.String("bucket") != "" {
|
||||
inputOptions = strings.Split(c.String("options"), ",")
|
||||
if secretKey == "" {
|
||||
errstr := `Partial credentials found in the env, missing : AWS_SECRET_ACCESS_KEY`
|
||||
return "", "", errors.New(errstr)
|
||||
}
|
||||
|
||||
if inputOptions[0] == "" {
|
||||
log.Fatal("options cannot be empty with a command name")
|
||||
}
|
||||
return commandName
|
||||
return accessKey, secretKey, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/minio-io/mc/pkg/s3"
|
||||
)
|
||||
|
||||
func parseGetObject(c *cli.Context) (bucket, key string, err error) {
|
||||
bucket = c.String("bucket")
|
||||
key = c.String("key")
|
||||
if bucket == "" {
|
||||
return "", "", errors.New("bucket name is mandatory")
|
||||
}
|
||||
if key == "" {
|
||||
return "", "", errors.New("object name is mandatory")
|
||||
}
|
||||
|
||||
return bucket, key, nil
|
||||
}
|
||||
|
||||
func doGetObject(c *cli.Context) {
|
||||
var bucket, key string
|
||||
var err error
|
||||
var objectReader io.ReadCloser
|
||||
var objectSize int64
|
||||
|
||||
accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
|
||||
secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
|
||||
if accessKey == "" && secretKey == "" {
|
||||
errstr := `You can configure your credentials by running "mc configure"`
|
||||
log.Fatal(errstr)
|
||||
}
|
||||
if accessKey == "" {
|
||||
errstr := `Partial credentials found in the env, missing : AWS_ACCESS_KEY_ID`
|
||||
log.Fatal(errstr)
|
||||
}
|
||||
|
||||
if secretKey == "" {
|
||||
errstr := `Partial credentials found in the env, missing : AWS_SECRET_ACCESS_KEY`
|
||||
log.Fatal(errstr)
|
||||
}
|
||||
|
||||
bucket, key, err = parseGetObject(c)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
s3c := s3.NewS3Client(accessKey, secretKey)
|
||||
objectReader, objectSize, err = s3c.Get(bucket, key)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
io.CopyN(os.Stdout, objectReader, objectSize)
|
||||
}
|
||||
+1
-1
@@ -125,7 +125,7 @@ func (c *Client) Stat(key, bucket string) (size int64, reterr error) {
|
||||
return 0, fmt.Errorf("s3: Unexpected status code %d statting object %v", res.StatusCode, key)
|
||||
}
|
||||
|
||||
func (c *Client) PutObject(key, bucket string, md5 hash.Hash, size int64, body io.Reader) error {
|
||||
func (c *Client) Put(bucket, key string, md5 hash.Hash, size int64, body io.Reader) error {
|
||||
req := newReq(c.keyURL(bucket, key))
|
||||
req.Method = "PUT"
|
||||
req.ContentLength = size
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package s3
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -28,9 +27,10 @@ var tc *Client
|
||||
|
||||
func getTestClient(t *testing.T) error {
|
||||
accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
|
||||
secret := os.Getenv("AWS_ACCESS_KEY_SECRET")
|
||||
secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
|
||||
if accessKey == "" || secret == "" {
|
||||
return errors.New("no AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY_SECRET set in environment")
|
||||
// skip the test if no AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY
|
||||
return nil
|
||||
}
|
||||
tc = NewS3Client(accessKey, secret)
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/minio-io/mc/pkg/s3"
|
||||
)
|
||||
|
||||
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 parsePutObject(c *cli.Context) (bucket, key, body string, err error) {
|
||||
bucket = c.String("bucket")
|
||||
key = c.String("key")
|
||||
body = c.String("body")
|
||||
|
||||
if bucket == "" {
|
||||
return "", "", "", errors.New("bucket name is mandatory")
|
||||
}
|
||||
|
||||
if key == "" {
|
||||
return "", "", "", errors.New("object name is mandatory")
|
||||
}
|
||||
|
||||
if body == "" {
|
||||
return "", "", "", errors.New("object blob is mandatory")
|
||||
}
|
||||
|
||||
return bucket, key, body, nil
|
||||
}
|
||||
|
||||
func doPutObject(c *cli.Context) {
|
||||
var err error
|
||||
var md5hash hash.Hash
|
||||
var accessKey, secretKey, bucket, key, body string
|
||||
accessKey, secretKey, err = getAWSEnvironment()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
bucket, key, body, err = parsePutObject(c)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
s3c := s3.NewS3Client(accessKey, secretKey)
|
||||
var bodyFile *os.File
|
||||
bodyFile, err = os.Open(body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var bodyBuffer io.Reader
|
||||
var size int64
|
||||
md5hash, bodyBuffer, size, err = getPutMetadata(bodyFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = s3c.Put(bucket, key, md5hash, size, bodyBuffer)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user