yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/objectstore/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 objectstore 16 17 import ( 18 "context" 19 "net/http" 20 "strings" 21 22 "yunion.io/x/log" 23 "yunion.io/x/pkg/errors" 24 "yunion.io/x/s3cli" 25 26 "yunion.io/x/cloudmux/pkg/cloudprovider" 27 ) 28 29 const ( 30 META_HEADER = "X-Amz-Meta-" 31 ) 32 33 type SObject struct { 34 bucket *SBucket 35 36 cloudprovider.SBaseCloudObject 37 } 38 39 func (o *SObject) GetIBucket() cloudprovider.ICloudBucket { 40 return o.bucket 41 } 42 43 func (o *SObject) GetAcl() cloudprovider.TBucketACLType { 44 acl, err := o.bucket.client.GetObjectAcl(o.bucket.Name, o.Key) 45 if err != nil { 46 if e, ok := errors.Cause(err).(s3cli.ErrorResponse); ok { 47 if e.Code == "NoSuchKey" || e.Message == "The specified key does not exist." { 48 objects, _ := o.bucket.ListObjects(o.Key, "", "", 1) 49 if len(objects.Objects) > 0 { 50 return cloudprovider.ACLPrivate 51 } 52 } 53 } 54 log.Errorf("o.bucket.client.GetObjectAcl error %s", err) 55 return cloudprovider.ACLPrivate 56 } 57 return acl 58 } 59 60 func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error { 61 err := o.bucket.client.SetObjectAcl(o.bucket.Name, o.Key, aclStr) 62 if err != nil { 63 if strings.Contains(err.Error(), "not implemented") || strings.Contains(err.Error(), "Please use AWS4-HMAC-SHA256") { 64 // ignore not implemented error 65 return nil // cloudprovider.ErrNotImplemented 66 } else { 67 return errors.Wrap(err, "o.bucket.client.SetObjectAcl") 68 } 69 } 70 return nil 71 } 72 73 func (o *SObject) GetMeta() http.Header { 74 if o.Meta != nil { 75 return o.Meta 76 } 77 cli := o.bucket.client.S3Client() 78 objInfo, err := cli.StatObject(o.bucket.Name, o.Key, s3cli.StatObjectOptions{}) 79 if err != nil { 80 log.Errorf("cli.statObject fail %s", err) 81 return nil 82 } 83 if len(objInfo.ContentType) > 0 { 84 objInfo.Metadata.Set(cloudprovider.META_HEADER_CONTENT_TYPE, objInfo.ContentType) 85 } 86 o.Meta = cloudprovider.FetchMetaFromHttpHeader(META_HEADER, objInfo.Metadata) 87 return o.Meta 88 } 89 90 func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error { 91 return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta) 92 }