yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/shell/object.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  	"io"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"os"
    23  	"time"
    24  
    25  	"yunion.io/x/pkg/errors"
    26  
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud/google"
    29  	"yunion.io/x/onecloud/pkg/util/shellutils"
    30  )
    31  
    32  func init() {
    33  	type ObjectPutOptions struct {
    34  		BUCKET      string
    35  		FILE        string
    36  		ContentType string
    37  		StorageType string `choices:"STANDARD|NEARLINE|COLDLINE|ARCHIVE"`
    38  		Acl         string `choices:"private|public-read|public-read-write|authenticated-read"`
    39  	}
    40  
    41  	shellutils.R(&ObjectPutOptions{}, "object-upload", "Upload object to buckets", func(cli *google.SRegion, args *ObjectPutOptions) error {
    42  		file, err := os.Open(args.FILE)
    43  		if err != nil {
    44  			return errors.Wrap(err, "so.Open")
    45  		}
    46  		stat, err := file.Stat()
    47  		if err != nil {
    48  			return errors.Wrap(err, "file.Stat")
    49  		}
    50  		header := http.Header{}
    51  		if len(args.ContentType) > 0 {
    52  			header.Set("Content-Type", args.ContentType)
    53  		}
    54  		if stat.Size() <= cloudprovider.MAX_PUT_OBJECT_SIZEBYTES {
    55  			return cli.PutObject(args.BUCKET, args.FILE, file, stat.Size(), cloudprovider.TBucketACLType(args.Acl), header)
    56  		}
    57  		uploadId, err := cli.NewMultipartUpload(args.BUCKET, args.FILE, cloudprovider.TBucketACLType(args.Acl), args.StorageType, http.Header{})
    58  		if err != nil {
    59  			return errors.Wrap(err, "NewMultipartUpload")
    60  		}
    61  		fmt.Println("uploadId: ", uploadId)
    62  		offset := int64(0)
    63  		partSize := cloudprovider.MAX_PUT_OBJECT_SIZEBYTES
    64  		count := stat.Size() / int64(partSize)
    65  		if stat.Size()%int64(partSize) > 0 {
    66  			count += 1
    67  		}
    68  		for i := 0; i < int(count); i++ {
    69  			err := cli.UploadPart(args.BUCKET, uploadId, 0, int64(offset), io.LimitReader(file, int64(partSize)), int64(partSize), stat.Size())
    70  			if err != nil {
    71  				return errors.Wrap(err, "UploadPart")
    72  			}
    73  			offset += partSize
    74  			if int64(offset+partSize) > stat.Size() {
    75  				partSize = stat.Size() % int64(partSize)
    76  			}
    77  		}
    78  		return nil
    79  	})
    80  
    81  	type ObjectListOptions struct {
    82  		BUCKET        string
    83  		Prefix        string
    84  		Delimiter     string
    85  		NextPageToken string
    86  		MaxResult     int
    87  	}
    88  
    89  	shellutils.R(&ObjectListOptions{}, "object-list", "List object from bucket", func(cli *google.SRegion, args *ObjectListOptions) error {
    90  		objs, err := cli.GetObjects(args.BUCKET, args.Prefix, args.NextPageToken, args.Delimiter, args.MaxResult)
    91  		if err != nil {
    92  			return err
    93  		}
    94  		printList(objs.Items, 0, 0, 0, nil)
    95  		return nil
    96  	})
    97  
    98  	type ObjectUrlOptions struct {
    99  		BUCKET string
   100  		OBJECT string
   101  		METHOD string
   102  		Hour   int
   103  	}
   104  
   105  	shellutils.R(&ObjectUrlOptions{}, "object-url", "Object temp url", func(cli *google.SRegion, args *ObjectUrlOptions) error {
   106  		url, err := cli.SingedUrl(args.BUCKET, args.OBJECT, args.METHOD, time.Duration(args.Hour)*time.Hour)
   107  		if err != nil {
   108  			return err
   109  		}
   110  		fmt.Println(url)
   111  		return nil
   112  	})
   113  
   114  	type ObjectOptions struct {
   115  		BUCKET string
   116  		OBJECT string
   117  	}
   118  
   119  	shellutils.R(&ObjectOptions{}, "object-acl-list", "List Object acl", func(cli *google.SRegion, args *ObjectOptions) error {
   120  		acls, err := cli.GetObjectAcl(args.BUCKET, args.OBJECT)
   121  		if err != nil {
   122  			return err
   123  		}
   124  		printList(acls, 0, 0, 0, nil)
   125  		return nil
   126  	})
   127  
   128  	shellutils.R(&ObjectOptions{}, "object-delete", "Delete Object", func(cli *google.SRegion, args *ObjectOptions) error {
   129  		return cli.DeleteObject(args.BUCKET, args.OBJECT)
   130  	})
   131  
   132  	type ObjectDownloadOptions struct {
   133  		BUCKET string
   134  		OBJECT string
   135  		Start  int64
   136  		End    int64
   137  	}
   138  
   139  	shellutils.R(&ObjectDownloadOptions{}, "object-download", "Download Object", func(cli *google.SRegion, args *ObjectDownloadOptions) error {
   140  		data, err := cli.DownloadObjectRange(args.BUCKET, args.OBJECT, args.Start, args.End)
   141  		if err != nil {
   142  			return errors.Wrap(err, "DownloadObjectRange")
   143  		}
   144  		content, err := ioutil.ReadAll(data)
   145  		if err != nil {
   146  			return errors.Wrap(err, "ioutil.ReadAll")
   147  		}
   148  		return ioutil.WriteFile(args.OBJECT, content, 0644)
   149  	})
   150  
   151  	type ObjectUploadCheckOptions struct {
   152  		BUCKET   string
   153  		UPLOADID string
   154  	}
   155  
   156  	shellutils.R(&ObjectUploadCheckOptions{}, "object-upload-check", "Upload object to buckets", func(cli *google.SRegion, args *ObjectUploadCheckOptions) error {
   157  		return cli.CheckUploadRange(args.BUCKET, args.UPLOADID)
   158  	})
   159  
   160  }