yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/obs"
    26  )
    27  
    28  type SObject struct {
    29  	bucket *SBucket
    30  
    31  	cloudprovider.SBaseCloudObject
    32  }
    33  
    34  func (o *SObject) GetIBucket() cloudprovider.ICloudBucket {
    35  	return o.bucket
    36  }
    37  
    38  func (o *SObject) GetAcl() cloudprovider.TBucketACLType {
    39  	acl := cloudprovider.ACLPrivate
    40  	obscli, err := o.bucket.region.getOBSClient()
    41  	if err != nil {
    42  		log.Errorf("o.bucket.region.GetOssClient error %s", err)
    43  		return acl
    44  	}
    45  	input := &obs.GetObjectAclInput{}
    46  	input.Bucket = o.bucket.Name
    47  	input.Key = o.Key
    48  	output, err := obscli.GetObjectAcl(input)
    49  	if err != nil {
    50  		log.Errorf("GetObjectAcl error: %v", err)
    51  		return acl
    52  	}
    53  	acl = obsAcl2CannedAcl(output.Grants)
    54  	return acl
    55  }
    56  
    57  func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error {
    58  	obscli, err := o.bucket.region.getOBSClient()
    59  	if err != nil {
    60  		return errors.Wrap(err, "o.bucket.region.getOBSClient")
    61  	}
    62  	input := &obs.SetObjectAclInput{}
    63  	input.Bucket = o.bucket.Name
    64  	input.Key = o.Key
    65  	input.ACL = obs.AclType(string(aclStr))
    66  	_, err = obscli.SetObjectAcl(input)
    67  	if err != nil {
    68  		return errors.Wrap(err, "obscli.SetObjectAcl")
    69  	}
    70  	return nil
    71  }
    72  
    73  func (o *SObject) GetMeta() http.Header {
    74  	if o.Meta != nil {
    75  		return o.Meta
    76  	}
    77  	obscli, err := o.bucket.region.getOBSClient()
    78  	if err != nil {
    79  		log.Errorf("getOBSClient fail %s", err)
    80  		return nil
    81  	}
    82  	input := &obs.GetObjectMetadataInput{}
    83  	input.Bucket = o.bucket.Name
    84  	input.Key = o.Key
    85  	output, err := obscli.GetObjectMetadata(input)
    86  	if err != nil {
    87  		log.Errorf("obscli.GetObjectMetadata fail %s", err)
    88  		return nil
    89  	}
    90  	meta := http.Header{}
    91  	for k, v := range output.Metadata {
    92  		meta.Add(k, v)
    93  	}
    94  	if len(output.ContentType) > 0 {
    95  		meta.Add(cloudprovider.META_HEADER_CONTENT_TYPE, output.ContentType)
    96  	}
    97  	o.Meta = meta
    98  	return meta
    99  }
   100  
   101  func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
   102  	return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta)
   103  }