object.go 2.4 KB

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