From f1377326b4a32ea8dbba0306f0e7d32953050088 Mon Sep 17 00:00:00 2001 From: TangBin Date: Mon, 12 Aug 2019 17:06:42 +0800 Subject: [PATCH] bucket object upload params change --- cmd/climc/shell/buckets.go | 23 +++++++++++++++++++---- pkg/mcclient/modules/mod_buckets.go | 12 +++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cmd/climc/shell/buckets.go b/cmd/climc/shell/buckets.go index 7b19eec90b..adb5e9febe 100644 --- a/cmd/climc/shell/buckets.go +++ b/cmd/climc/shell/buckets.go @@ -15,6 +15,7 @@ package shell import ( + "fmt" "io" "os" @@ -154,10 +155,12 @@ func init() { type BucketUploadObjectsOptions struct { ID string `help:"ID or name of bucket" json:"-"` KEY string `help:"Key of object to upload"` - Path string `help:"Path to file to upload"` + Path string `help:"Path to file to upload" required:"true"` - ContentType string `help:"Content type"` - StorageClass string `help:"storage CLass"` + ContentLength int64 `help:"Content lenght (bytes)"` + ContentType string `help:"Content type" required:"true"` + StorageClass string `help:"storage CLass"` + Acl string `help:"object acl." choices:"private|public-read|public-read-write"` } R(&BucketUploadObjectsOptions{}, "bucket-object-upload", "Upload an object into a bucket", func(s *mcclient.ClientSession, args *BucketUploadObjectsOptions) error { var body io.Reader @@ -168,10 +171,22 @@ func init() { } defer file.Close() body = file + + fileInfo, err := file.Stat() + if err != nil { + return err + } + + args.ContentLength = fileInfo.Size() } else { body = os.Stdin } - err := modules.Buckets.Upload(s, args.ID, args.KEY, body, args.ContentType, args.StorageClass) + + if args.ContentLength <= 0 { + return fmt.Errorf("required content-length") + } + + err := modules.Buckets.Upload(s, args.ID, args.KEY, body, args.ContentLength, args.ContentType, args.StorageClass, args.Acl) if err != nil { return err } diff --git a/pkg/mcclient/modules/mod_buckets.go b/pkg/mcclient/modules/mod_buckets.go index bc3284dd6b..674aea601f 100644 --- a/pkg/mcclient/modules/mod_buckets.go +++ b/pkg/mcclient/modules/mod_buckets.go @@ -18,6 +18,7 @@ import ( "fmt" "io" "net/http" + "strconv" "github.com/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -29,7 +30,7 @@ type SBucketManager struct { ResourceManager } -func (manager *SBucketManager) Upload(s *mcclient.ClientSession, bucketId string, key string, body io.Reader, contType string, storageClass string) error { +func (manager *SBucketManager) Upload(s *mcclient.ClientSession, bucketId string, key string, body io.Reader, contLength int64, contType string, storageClass string, acl string) error { method := httputils.POST path := fmt.Sprintf("/%s/%s/upload", manager.URLPath(), bucketId) headers := http.Header{} @@ -37,10 +38,19 @@ func (manager *SBucketManager) Upload(s *mcclient.ClientSession, bucketId string if len(contType) > 0 { headers.Set("Content-Type", contType) } + + if contLength > 0 { + headers.Set("Content-Length", strconv.FormatInt(contLength, 10)) + } + if len(storageClass) > 0 { headers.Set(api.BUCKET_UPLOAD_OBJECT_STORAGECLASS_HEADER, storageClass) } + if len(acl) > 0 { + headers.Set(api.BUCKET_UPLOAD_OBJECT_ACL_HEADER, acl) + } + _, err := manager.rawRequest(s, method, path, headers, body) if err != nil { return errors.Wrap(err, "rawRequest")