yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/blobobject.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 azure 16 17 import ( 18 "context" 19 "net/http" 20 21 "github.com/Azure/azure-sdk-for-go/storage" 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 container *SContainer 31 32 cloudprovider.SBaseCloudObject 33 } 34 35 func (o *SObject) GetIBucket() cloudprovider.ICloudBucket { 36 return o.container.storageaccount 37 } 38 39 func (o *SObject) GetAcl() cloudprovider.TBucketACLType { 40 return o.container.getAcl() 41 } 42 43 func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error { 44 return nil 45 } 46 47 func (o *SObject) getBlobName() string { 48 if len(o.Key) <= len(o.container.Name)+1 { 49 return "" 50 } else { 51 return o.Key[len(o.container.Name)+1:] 52 } 53 } 54 55 func (o *SObject) getBlobRef() (*storage.Blob, error) { 56 blobName := o.getBlobName() 57 if len(blobName) == 0 { 58 return nil, nil 59 } 60 contRef, err := o.container.getContainerRef() 61 if err != nil { 62 return nil, errors.Wrap(err, "src getContainerRef") 63 } 64 blobRef := contRef.GetBlobReference(blobName) 65 return blobRef, nil 66 } 67 68 func (o *SObject) GetMeta() http.Header { 69 if o.Meta != nil { 70 return o.Meta 71 } 72 blobRef, err := o.getBlobRef() 73 if err != nil { 74 log.Errorf("o.getBlobRef fail %s", err) 75 return nil 76 } 77 if blobRef == nil { 78 return nil 79 } 80 err = blobRef.GetMetadata(nil) 81 if err != nil { 82 log.Errorf("blobRef.GetMetadata fail %s", err) 83 } 84 err = blobRef.GetProperties(nil) 85 if err != nil { 86 log.Errorf("blobRef.GetProperties fail %s", err) 87 } 88 meta := getBlobRefMeta(blobRef) 89 o.Meta = meta 90 return o.Meta 91 } 92 93 func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error { 94 blobRef, err := o.getBlobRef() 95 if err != nil { 96 return errors.Wrap(err, "o.getBlobRef") 97 } 98 if blobRef == nil { 99 return cloudprovider.ErrNotSupported 100 } 101 propChanged, metaChanged := setBlobRefMeta(blobRef, meta) 102 if propChanged { 103 propOpts := storage.SetBlobPropertiesOptions{} 104 err := blobRef.SetProperties(&propOpts) 105 if err != nil { 106 return errors.Wrap(err, "blob.SetProperties") 107 } 108 } 109 if metaChanged { 110 metaOpts := storage.SetBlobMetadataOptions{} 111 err := blobRef.SetMetadata(&metaOpts) 112 if err != nil { 113 return errors.Wrap(err, "blob.SetMetadata") 114 } 115 } 116 return nil 117 }