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