objects.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2023 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 volcengine
  15. import (
  16. "context"
  17. "net/http"
  18. "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
  19. "github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. const (
  25. OSS_META_HEADER = "x-oss-meta-"
  26. )
  27. type SObject struct {
  28. bucket *SBucket
  29. cloudprovider.SBaseCloudObject
  30. }
  31. func (obj *SObject) GetIBucket() cloudprovider.ICloudBucket {
  32. return obj.bucket
  33. }
  34. func (obj *SObject) GetAcl() cloudprovider.TBucketACLType {
  35. acl := cloudprovider.ACLPrivate
  36. toscli, err := obj.bucket.region.GetTosClient()
  37. if err != nil {
  38. log.Errorf("Get Client %s", err)
  39. return acl
  40. }
  41. result, err := toscli.GetObjectACL(context.Background(), &tos.GetObjectACLInput{Bucket: obj.bucket.Name, Key: obj.Key})
  42. if err != nil {
  43. log.Errorf("GetObjectAcl %s", err)
  44. }
  45. grants := result.Grants
  46. return grantToCannedAcl(grants)
  47. }
  48. func (obj *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error {
  49. toscli, err := obj.bucket.region.GetTosClient()
  50. if err != nil {
  51. return errors.Wrap(err, "GetTosClient")
  52. }
  53. input := &tos.PutObjectACLInput{Bucket: obj.bucket.Name, Key: obj.Key, ACL: enum.ACLType(aclStr)}
  54. _, err = toscli.PutObjectACL(context.Background(), input)
  55. if err != nil {
  56. return errors.Wrapf(err, "PutObjectACL")
  57. }
  58. return nil
  59. }
  60. func (obj *SObject) GetMeta() http.Header {
  61. if obj.Meta != nil {
  62. return obj.Meta
  63. }
  64. toscli, err := obj.bucket.region.GetTosClient()
  65. if err != nil {
  66. log.Errorf("Get Client %s", err)
  67. return nil
  68. }
  69. result, err := toscli.GetObjectV2(context.Background(), &tos.GetObjectV2Input{Bucket: obj.bucket.Name, Key: obj.Key})
  70. if err != nil {
  71. log.Errorf("Get Object error %s", err)
  72. return nil
  73. }
  74. newHeader := http.Header{}
  75. meta := result.GetObjectBasicOutput.ObjectMetaV2.Meta
  76. for _, key := range meta.AllKeys() {
  77. value, exist := meta.Get(key)
  78. if !exist {
  79. log.Errorf("Key missing in meta data %s", key)
  80. } else {
  81. newHeader.Add(key, value)
  82. }
  83. }
  84. obj.Meta = cloudprovider.FetchMetaFromHttpHeader(
  85. OSS_META_HEADER,
  86. newHeader,
  87. )
  88. return obj.Meta
  89. }
  90. func (obj *SObject) SetMeta(ctx context.Context, meta http.Header) error {
  91. return cloudprovider.ObjectSetMeta(ctx, obj.bucket, obj, meta)
  92. }