yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/shell/oss.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package shell
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	osslib "github.com/aliyun/aliyun-oss-go-sdk/oss"
    23  
    24  	"yunion.io/x/cloudmux/pkg/multicloud/aliyun"
    25  	"yunion.io/x/onecloud/pkg/util/fileutils2"
    26  	"yunion.io/x/onecloud/pkg/util/shellutils"
    27  )
    28  
    29  type progressListener struct {
    30  }
    31  
    32  func (this *progressListener) ProgressChanged(event *osslib.ProgressEvent) {
    33  	switch event.EventType {
    34  	case osslib.TransferStartedEvent:
    35  		fmt.Printf("\n")
    36  	case osslib.TransferDataEvent:
    37  		fmt.Printf("Progess: %f%%\r", (float64(event.ConsumedBytes) * 100.0 / float64(event.TotalBytes)))
    38  	case osslib.TransferCompletedEvent:
    39  		fmt.Printf("Transfer complete!\n")
    40  	case osslib.TransferFailedEvent:
    41  		fmt.Printf("Transfer failed!\n")
    42  	default:
    43  		fmt.Printf("Unknonw event type %d\n", event.EventType)
    44  	}
    45  }
    46  
    47  func str2AclType(aclStr string) osslib.ACLType {
    48  	switch aclStr {
    49  	case "public-rw":
    50  		return osslib.ACLPublicReadWrite
    51  	case "public-read":
    52  		return osslib.ACLPublicRead
    53  	default:
    54  		return osslib.ACLPrivate
    55  	}
    56  }
    57  
    58  func init() {
    59  	type OssListOptions struct {
    60  	}
    61  	shellutils.R(&OssListOptions{}, "oss-list", "List OSS buckets", func(cli *aliyun.SRegion, args *OssListOptions) error {
    62  		buckets, err := cli.GetIBuckets()
    63  		if err != nil {
    64  			return err
    65  		}
    66  		printList(buckets, len(buckets), 0, 50, nil)
    67  		return nil
    68  	})
    69  
    70  	type OssListBucketOptions struct {
    71  		BUCKET string `help:"bucket name"`
    72  	}
    73  
    74  	shellutils.R(&OssListBucketOptions{}, "oss-list-bucket", "List content of a OSS bucket", func(cli *aliyun.SRegion, args *OssListBucketOptions) error {
    75  		oss, err := cli.GetOssClient()
    76  		if err != nil {
    77  			return err
    78  		}
    79  		bucket, err := oss.Bucket(args.BUCKET)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		result, err := bucket.ListObjects()
    84  		if err != nil {
    85  			return err
    86  		}
    87  		printList(result.Objects, len(result.Objects), 0, len(result.Objects), nil)
    88  		return nil
    89  	})
    90  
    91  	type OssCreateBucketOptions struct {
    92  		BUCKET       string `help:"bucket name"`
    93  		StorageClass string `help:"storage class" choices:"Standard|IA|Archive"`
    94  
    95  		Acl string `help:"ACL" choices:"private|public-read|public-read-write"`
    96  	}
    97  	shellutils.R(&OssCreateBucketOptions{}, "oss-create-bucket", "Create a OSS bucket", func(cli *aliyun.SRegion, args *OssCreateBucketOptions) error {
    98  		err := cli.CreateIBucket(args.BUCKET, args.StorageClass, args.Acl)
    99  		if err != nil {
   100  			return err
   101  		}
   102  		return nil
   103  	})
   104  
   105  	type OssDeleteBucketOptions struct {
   106  		BUCKET string `help:"bucket name"`
   107  	}
   108  	shellutils.R(&OssDeleteBucketOptions{}, "oss-delete-bucket", "Delete a OSS bucket", func(cli *aliyun.SRegion, args *OssDeleteBucketOptions) error {
   109  		err := cli.DeleteIBucket(args.BUCKET)
   110  		if err != nil {
   111  			return err
   112  		}
   113  		return nil
   114  	})
   115  
   116  	type OssUploadOptions struct {
   117  		BUCKET   string `help:"bucket name"`
   118  		KEY      string `help:"Object key"`
   119  		FILE     string `help:"Local file path"`
   120  		Progress bool   `help:"show progress"`
   121  		Acl      string `help:"Object ACL" choices:"private|public-read|public-rw"`
   122  	}
   123  	shellutils.R(&OssUploadOptions{}, "oss-upload", "Upload a file to a OSS bucket", func(cli *aliyun.SRegion, args *OssUploadOptions) error {
   124  		oss, err := cli.GetOssClient()
   125  		if err != nil {
   126  			return err
   127  		}
   128  		bucket, err := oss.Bucket(args.BUCKET)
   129  		if err != nil {
   130  			return err
   131  		}
   132  
   133  		options := make([]osslib.Option, 0)
   134  		if args.Progress {
   135  			listener := progressListener{}
   136  			options = append(options, osslib.Progress(&listener))
   137  		}
   138  		if len(args.Acl) > 0 {
   139  			options = append(options, osslib.ObjectACL(str2AclType(args.Acl)))
   140  		}
   141  		if fileutils2.IsFile(args.FILE) {
   142  			err = bucket.UploadFile(args.KEY, args.FILE, 4*1024*1024, options...)
   143  			return err
   144  		} else if fileutils2.IsDir(args.FILE) {
   145  			return filepath.Walk(args.FILE, func(path string, info os.FileInfo, err error) error {
   146  				if err != nil {
   147  					return err
   148  				}
   149  				if info.Mode().IsRegular() {
   150  					rel, _ := filepath.Rel(args.FILE, path)
   151  					src := path
   152  					dst := filepath.Join(args.KEY, rel)
   153  					fmt.Println("upload", src, "to", dst)
   154  					uploadErr := bucket.UploadFile(dst, src,
   155  						4*1024*1024, options...)
   156  					if uploadErr != nil {
   157  						return uploadErr
   158  					}
   159  				}
   160  				return nil
   161  			})
   162  		} else {
   163  			return fmt.Errorf("Unsupported file type %s", args.FILE)
   164  		}
   165  	})
   166  
   167  	type OssObjectAclOptions struct {
   168  		BUCKET string `help:"bucket name"`
   169  		KEY    string `help:"object key"`
   170  		ACL    string `help:"ACL" choices:"private|public-read|public-rw"`
   171  	}
   172  	shellutils.R(&OssObjectAclOptions{}, "oss-set-acl", "Set acl for a object", func(cli *aliyun.SRegion, args *OssObjectAclOptions) error {
   173  		oss, err := cli.GetOssClient()
   174  		if err != nil {
   175  			return err
   176  		}
   177  		bucket, err := oss.Bucket(args.BUCKET)
   178  		if err != nil {
   179  			return err
   180  		}
   181  		err = bucket.SetObjectACL(args.KEY, str2AclType(args.ACL))
   182  		return err
   183  	})
   184  
   185  	type OssDeleteOptions struct {
   186  		BUCKET string `help:"bucket name"`
   187  		KEY    string `help:"Object key"`
   188  	}
   189  
   190  	shellutils.R(&OssDeleteOptions{}, "oss-delete", "Delete a file from a OSS bucket", func(cli *aliyun.SRegion, args *OssDeleteOptions) error {
   191  		oss, err := cli.GetOssClient()
   192  		if err != nil {
   193  			return err
   194  		}
   195  		bucket, err := oss.Bucket(args.BUCKET)
   196  		if err != nil {
   197  			return err
   198  		}
   199  		err = bucket.DeleteObject(args.KEY)
   200  		return err
   201  	})
   202  }