yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"github.com/tencentyun/cos-go-sdk-v5"
    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  	coscli, err := o.bucket.region.GetCosClient(o.bucket)
    42  	if err != nil {
    43  		log.Errorf("o.bucket.region.GetOssClient error %s", err)
    44  		return acl
    45  	}
    46  	result, _, err := coscli.Object.GetACL(context.Background(), o.Key)
    47  	if err != nil {
    48  		log.Errorf("coscli.Object.GetACL error %s", err)
    49  		return acl
    50  	}
    51  	return cosAcl2CannedAcl(result.AccessControlList)
    52  }
    53  
    54  func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error {
    55  	coscli, err := o.bucket.region.GetCosClient(o.bucket)
    56  	if err != nil {
    57  		return errors.Wrap(err, "o.bucket.region.GetCosClient")
    58  	}
    59  	opts := &cos.ObjectPutACLOptions{
    60  		Header: &cos.ACLHeaderOptions{},
    61  	}
    62  	opts.Header.XCosACL = string(aclStr)
    63  	_, err = coscli.Object.PutACL(context.Background(), o.Key, opts)
    64  	if err != nil {
    65  		return errors.Wrap(err, "coscli.Object.PutACL")
    66  	}
    67  	return nil
    68  }
    69  
    70  func (o *SObject) GetMeta() http.Header {
    71  	if o.Meta != nil {
    72  		return o.Meta
    73  	}
    74  	coscli, err := o.bucket.region.GetCosClient(o.bucket)
    75  	if err != nil {
    76  		log.Errorf("o.bucket.region.GetCosClient fail %s", err)
    77  		return nil
    78  	}
    79  	resp, err := coscli.Object.Head(context.Background(), o.Key, nil)
    80  	if err != nil {
    81  		log.Errorf("coscli.Object.Head fail %s", err)
    82  		return nil
    83  	}
    84  	o.Meta = cloudprovider.FetchMetaFromHttpHeader(COS_META_HEADER, resp.Header)
    85  	return o.Meta
    86  }
    87  
    88  func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
    89  	return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta)
    90  }