yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/objects.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 aliyun
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    22  
    23  	"yunion.io/x/log"
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  )
    28  
    29  const (
    30  	OSS_META_HEADER = "x-oss-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 := cloudprovider.ACLPrivate
    45  	osscli, err := o.bucket.region.GetOssClient()
    46  	if err != nil {
    47  		log.Errorf("o.bucket.region.GetOssClient error %s", err)
    48  		return acl
    49  	}
    50  	bucket, err := osscli.Bucket(o.bucket.Name)
    51  	if err != nil {
    52  		log.Errorf("osscli.Bucket error %s", err)
    53  		return acl
    54  	}
    55  	result, err := bucket.GetObjectACL(o.Key)
    56  	if err != nil {
    57  		log.Errorf("bucket.GetObjectACL error %s", err)
    58  		return acl
    59  	}
    60  	if result.ACL == string(oss.ACLDefault) {
    61  		return o.bucket.GetAcl()
    62  	}
    63  	acl = cloudprovider.TBucketACLType(result.ACL)
    64  	return acl
    65  }
    66  
    67  func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error {
    68  	acl, err := str2Acl(string(aclStr))
    69  	if err != nil {
    70  		return errors.Wrap(err, "str2Acl")
    71  	}
    72  	osscli, err := o.bucket.region.GetOssClient()
    73  	if err != nil {
    74  		return errors.Wrap(err, "o.bucket.region.GetOssClient")
    75  	}
    76  	bucket, err := osscli.Bucket(o.bucket.Name)
    77  	if err != nil {
    78  		return errors.Wrap(err, "osscli.Bucket")
    79  	}
    80  	err = bucket.SetObjectACL(o.Key, acl)
    81  	if err != nil {
    82  		return errors.Wrap(err, "bucket.SetObjectACL")
    83  	}
    84  	return nil
    85  }
    86  
    87  func (o *SObject) GetMeta() http.Header {
    88  	if o.Meta != nil {
    89  		return o.Meta
    90  	}
    91  	osscli, err := o.bucket.region.GetOssClient()
    92  	if err != nil {
    93  		log.Errorf("o.bucket.region.GetOssClient error %s", err)
    94  		return nil
    95  	}
    96  	bucket, err := osscli.Bucket(o.bucket.Name)
    97  	if err != nil {
    98  		log.Errorf("osscli.Bucket error %s", err)
    99  		return nil
   100  	}
   101  	result, err := bucket.GetObjectDetailedMeta(o.Key)
   102  	if err != nil {
   103  		log.Errorf("bucket.GetObjectACL error %s", err)
   104  		return nil
   105  	}
   106  	o.Meta = cloudprovider.FetchMetaFromHttpHeader(OSS_META_HEADER, result)
   107  	return o.Meta
   108  }
   109  
   110  func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
   111  	return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta)
   112  }