object.go 2.6 KB

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