yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/shell/s3.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 21 "github.com/aws/aws-sdk-go/service/s3" 22 23 "yunion.io/x/cloudmux/pkg/multicloud/aws" 24 "yunion.io/x/onecloud/pkg/util/printutils" 25 "yunion.io/x/onecloud/pkg/util/shellutils" 26 "yunion.io/x/onecloud/pkg/util/streamutils" 27 ) 28 29 func init() { 30 type S3BucketListOptions struct { 31 } 32 shellutils.R(&S3BucketListOptions{}, "s3-list", "List all buckets", func(cli *aws.SRegion, args *S3BucketListOptions) error { 33 buckets, err := cli.GetIBuckets() 34 if err != nil { 35 return err 36 } 37 printList(buckets, 0, 0, 0, nil) 38 printutils.PrintGetterList(buckets, nil) 39 return nil 40 }) 41 42 type S3CreateBucketOptions struct { 43 BUCKET string `help:"bucket name"` 44 } 45 shellutils.R(&S3CreateBucketOptions{}, "s3-create-bucket", "Create a bucket", func(cli *aws.SRegion, args *S3CreateBucketOptions) error { 46 err := cli.CreateIBucket(args.BUCKET, "", "") 47 if err != nil { 48 return err 49 } 50 return nil 51 }) 52 53 type S3DeleteBucketOptions struct { 54 BUCKET string `help:"bucket name"` 55 } 56 shellutils.R(&S3DeleteBucketOptions{}, "s3-delete-bucket", "Delete a bucket", func(cli *aws.SRegion, args *S3DeleteBucketOptions) error { 57 err := cli.DeleteIBucket(args.BUCKET) 58 if err != nil { 59 return err 60 } 61 return nil 62 }) 63 64 type S3ListObjectOptions struct { 65 BUCKET string 66 Prefix string 67 Marker string 68 Limit int64 `help:"list limit" default:"20"` 69 } 70 shellutils.R(&S3ListObjectOptions{}, "s3-list-objects", "List objects in a bucket", func(cli *aws.SRegion, args *S3ListObjectOptions) error { 71 s3cli, err := cli.GetS3Client() 72 if err != nil { 73 return err 74 } 75 input := &s3.ListObjectsInput{} 76 input = input.SetBucket(args.BUCKET) 77 if args.Limit > 0 { 78 input = input.SetMaxKeys(args.Limit) 79 } 80 if len(args.Prefix) > 0 { 81 input = input.SetPrefix(args.Prefix) 82 } 83 if len(args.Marker) > 0 { 84 input = input.SetMarker(args.Marker) 85 } 86 87 output, err := s3cli.ListObjects(input) 88 if err != nil { 89 return err 90 } 91 printList(output.Contents, 0, 0, 0, nil) 92 if output.IsTruncated != nil && *output.IsTruncated { 93 fmt.Println("More ...") 94 } 95 return nil 96 }) 97 98 type S3DownloadObjectOptions struct { 99 BUCKET string `help:"Bucket name"` 100 OBJECT string `help:"Object key"` 101 Output string `help:"Location of output file"` 102 } 103 shellutils.R(&S3DownloadObjectOptions{}, "s3-download", "Download an object in a bucket", func(cli *aws.SRegion, args *S3DownloadObjectOptions) error { 104 s3cli, err := cli.GetS3Client() 105 if err != nil { 106 return err 107 } 108 input := &s3.GetObjectInput{} 109 input = input.SetBucket(args.BUCKET).SetKey(args.OBJECT) 110 output, err := s3cli.GetObject(input) 111 if err != nil { 112 return err 113 } 114 var fio *os.File 115 if len(args.Output) > 0 { 116 fio, err = os.Create(args.Output) 117 if err != nil { 118 return err 119 } 120 defer fio.Close() 121 } else { 122 fio = os.Stdout 123 } 124 prop, err := streamutils.StreamPipe(output.Body, fio, true, nil) 125 if err != nil { 126 return err 127 } 128 if len(args.Output) > 0 { 129 fmt.Printf("File: %s Size: %d Chksum: %s\n", args.Output, prop.Size, prop.CheckSum) 130 } 131 return nil 132 }) 133 }