bucket object upload params change

This commit is contained in:
TangBin
2019-08-12 19:12:40 +08:00
parent dc73e3cb9e
commit f1377326b4
2 changed files with 30 additions and 5 deletions
+19 -4
View File
@@ -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
}
+11 -1
View File
@@ -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")