blobobject.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 azure
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "net/url"
  20. "strings"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/utils"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. )
  25. type SObject struct {
  26. container *SContainer
  27. cloudprovider.SBaseCloudObject
  28. }
  29. func (o *SObject) GetIBucket() cloudprovider.ICloudBucket {
  30. return o.container.storageaccount
  31. }
  32. func (o *SObject) GetAcl() cloudprovider.TBucketACLType {
  33. return o.container.getAcl()
  34. }
  35. func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error {
  36. return cloudprovider.ErrNotSupported
  37. }
  38. func (o *SObject) getBlobName() string {
  39. if len(o.Key) <= len(o.container.Name)+1 {
  40. return ""
  41. } else {
  42. return o.Key[len(o.container.Name)+1:]
  43. }
  44. }
  45. func (sa *SStorageAccount) GetObjectMeta(object string) (http.Header, error) {
  46. accessKey, err := sa.GetAccountKey()
  47. if err != nil {
  48. return nil, errors.Wrap(err, "GetAccountKey")
  49. }
  50. ret := http.Header{}
  51. params := url.Values{}
  52. header, err := sa.region.header_storage_v2(accessKey, sa.Name, object, params)
  53. if err != nil {
  54. return nil, errors.Wrap(err, "header_storage_v2")
  55. }
  56. for k := range header {
  57. if strings.HasPrefix(strings.ToLower(k), "x-ms-meta-") {
  58. ret.Add(strings.TrimPrefix(strings.ToLower(k), "x-ms-meta-"), header.Get(k))
  59. }
  60. if utils.IsInStringArray(k, []string{
  61. http.CanonicalHeaderKey(cloudprovider.META_HEADER_CACHE_CONTROL),
  62. http.CanonicalHeaderKey(cloudprovider.META_HEADER_CONTENT_TYPE),
  63. http.CanonicalHeaderKey(cloudprovider.META_HEADER_CONTENT_DISPOSITION),
  64. http.CanonicalHeaderKey(cloudprovider.META_HEADER_CONTENT_ENCODING),
  65. http.CanonicalHeaderKey(cloudprovider.META_HEADER_CONTENT_LANGUAGE),
  66. http.CanonicalHeaderKey(cloudprovider.META_HEADER_CONTENT_MD5),
  67. }) {
  68. ret.Set(k, header.Get(k))
  69. }
  70. }
  71. return ret, nil
  72. }
  73. func (o *SObject) GetMeta() http.Header {
  74. if o.Meta != nil {
  75. return o.Meta
  76. }
  77. objectName := fmt.Sprintf("%s/%s", o.container.Name, o.getBlobName())
  78. var err error
  79. meta, err := o.container.storageaccount.GetObjectMeta(objectName)
  80. if err != nil {
  81. return nil
  82. }
  83. o.Meta = meta
  84. return meta
  85. }
  86. func (sa *SStorageAccount) SetObjectMeta(ctx context.Context, object string, meta http.Header) error {
  87. accessKey, err := sa.GetAccountKey()
  88. if err != nil {
  89. return errors.Wrap(err, "GetAccountKey")
  90. }
  91. properties := http.Header{}
  92. metadata := http.Header{}
  93. for k := range meta {
  94. if utils.IsInStringArray(k, []string{
  95. cloudprovider.META_HEADER_CACHE_CONTROL,
  96. cloudprovider.META_HEADER_CONTENT_TYPE,
  97. cloudprovider.META_HEADER_CONTENT_DISPOSITION,
  98. cloudprovider.META_HEADER_CONTENT_ENCODING,
  99. cloudprovider.META_HEADER_CONTENT_LANGUAGE,
  100. cloudprovider.META_HEADER_CONTENT_MD5,
  101. }) {
  102. properties.Set(fmt.Sprintf("x-ms-blob-%s", strings.ToLower(k)), meta.Get(k))
  103. } else {
  104. metadata.Set(fmt.Sprintf("x-ms-meta-%s", k), meta.Get(k))
  105. }
  106. }
  107. if len(properties) > 0 {
  108. params := url.Values{}
  109. params.Set("comp", "properties")
  110. err = sa.region.client.put_storage_v2(accessKey, sa.Name, object, properties, params, nil, nil)
  111. if err != nil {
  112. return errors.Wrap(err, "put_storage_v2 set properties")
  113. }
  114. }
  115. if len(metadata) > 0 {
  116. params := url.Values{}
  117. params.Set("comp", "metadata")
  118. err = sa.region.client.put_storage_v2(accessKey, sa.Name, object, metadata, params, nil, nil)
  119. if err != nil {
  120. return errors.Wrap(err, "put_storage_v2 set metadata")
  121. }
  122. }
  123. return nil
  124. }
  125. func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
  126. fileName := fmt.Sprintf("%s/%s", o.container.Name, o.getBlobName())
  127. return o.container.storageaccount.SetObjectMeta(ctx, fileName, meta)
  128. }