yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/s3object.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 aws 16 17 import ( 18 "context" 19 "net/http" 20 21 "github.com/aws/aws-sdk-go/service/s3" 22 23 "yunion.io/x/log" 24 "yunion.io/x/pkg/errors" 25 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 ) 28 29 type SObject struct { 30 bucket *SBucket 31 32 cloudprovider.SBaseCloudObject 33 } 34 35 func (o *SObject) GetIBucket() cloudprovider.ICloudBucket { 36 return o.bucket 37 } 38 39 func (o *SObject) GetAcl() cloudprovider.TBucketACLType { 40 acl := cloudprovider.ACLPrivate 41 s3cli, err := o.bucket.region.GetS3Client() 42 if err != nil { 43 log.Errorf("o.bucket.region.GetS3Client error %s", err) 44 return acl 45 } 46 input := &s3.GetObjectAclInput{} 47 input.SetBucket(o.bucket.Name) 48 input.SetKey(o.Key) 49 output, err := s3cli.GetObjectAcl(input) 50 if err != nil { 51 log.Errorf("s3cli.GetObjectAcl error %s", err) 52 return acl 53 } 54 return s3ToCannedAcl(output.Grants) 55 } 56 57 func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error { 58 s3cli, err := o.bucket.region.GetS3Client() 59 if err != nil { 60 return errors.Wrap(err, "o.bucket.region.GetS3Client") 61 } 62 input := &s3.PutObjectAclInput{} 63 input.SetBucket(o.bucket.Name) 64 input.SetKey(o.Key) 65 input.SetACL(string(aclStr)) 66 _, err = s3cli.PutObjectAcl(input) 67 if err != nil { 68 return errors.Wrap(err, "s3cli.PutObjectAcl") 69 } 70 return nil 71 } 72 73 func (o *SObject) GetMeta() http.Header { 74 if o.Meta != nil { 75 return o.Meta 76 } 77 s3cli, err := o.bucket.region.GetS3Client() 78 if err != nil { 79 log.Errorf("o.bucket.region.GetS3Client fail %s", err) 80 return nil 81 } 82 input := &s3.HeadObjectInput{} 83 input.SetBucket(o.bucket.Name) 84 input.SetKey(o.Key) 85 output, err := s3cli.HeadObject(input) 86 if err != nil { 87 log.Errorf("s3cli.HeadObject fail %s", err) 88 return nil 89 } 90 ret := http.Header{} 91 for k, v := range output.Metadata { 92 if v != nil && len(*v) > 0 { 93 ret.Add(k, *v) 94 } 95 } 96 if output.CacheControl != nil && len(*output.CacheControl) > 0 { 97 ret.Set(cloudprovider.META_HEADER_CACHE_CONTROL, *output.CacheControl) 98 } 99 if output.ContentType != nil && len(*output.ContentType) > 0 { 100 ret.Set(cloudprovider.META_HEADER_CONTENT_TYPE, *output.ContentType) 101 } 102 if output.ContentDisposition != nil && len(*output.ContentDisposition) > 0 { 103 ret.Set(cloudprovider.META_HEADER_CONTENT_DISPOSITION, *output.ContentDisposition) 104 } 105 if output.ContentEncoding != nil && len(*output.ContentEncoding) > 0 { 106 ret.Set(cloudprovider.META_HEADER_CONTENT_ENCODING, *output.ContentEncoding) 107 } 108 if output.ContentLanguage != nil && len(*output.ContentLanguage) > 0 { 109 ret.Set(cloudprovider.META_HEADER_CONTENT_LANGUAGE, *output.ContentLanguage) 110 } 111 return ret 112 } 113 114 func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error { 115 return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta) 116 }