s3object.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 aws
  15. import (
  16. "context"
  17. "net/http"
  18. "github.com/aws/aws-sdk-go-v2/service/s3"
  19. "github.com/aws/aws-sdk-go-v2/service/s3/types"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. )
  24. type SObject struct {
  25. bucket *SBucket
  26. cloudprovider.SBaseCloudObject
  27. }
  28. func (o *SObject) GetIBucket() cloudprovider.ICloudBucket {
  29. return o.bucket
  30. }
  31. func (o *SObject) GetAcl() cloudprovider.TBucketACLType {
  32. acl := cloudprovider.ACLPrivate
  33. s3cli, err := o.bucket.region.GetS3Client()
  34. if err != nil {
  35. log.Errorf("o.bucket.region.GetS3Client error %s", err)
  36. return acl
  37. }
  38. input := &s3.GetObjectAclInput{
  39. Bucket: &o.bucket.Name,
  40. Key: &o.Key,
  41. }
  42. output, err := s3cli.GetObjectAcl(context.Background(), input)
  43. if err != nil {
  44. log.Errorf("s3cli.GetObjectAcl error %s", err)
  45. return acl
  46. }
  47. return s3ToCannedAcl(output.Grants)
  48. }
  49. func (o *SObject) SetAcl(aclStr cloudprovider.TBucketACLType) error {
  50. s3cli, err := o.bucket.region.GetS3Client()
  51. if err != nil {
  52. return errors.Wrap(err, "o.bucket.region.GetS3Client")
  53. }
  54. input := &s3.PutObjectAclInput{
  55. Bucket: &o.bucket.Name,
  56. Key: &o.Key,
  57. ACL: types.ObjectCannedACL(string(aclStr)),
  58. }
  59. _, err = s3cli.PutObjectAcl(context.Background(), input)
  60. if err != nil {
  61. return errors.Wrap(err, "s3cli.PutObjectAcl")
  62. }
  63. return nil
  64. }
  65. func (o *SObject) GetMeta() http.Header {
  66. if o.Meta != nil {
  67. return o.Meta
  68. }
  69. s3cli, err := o.bucket.region.GetS3Client()
  70. if err != nil {
  71. log.Errorf("GetS3Client fail %s", err)
  72. return nil
  73. }
  74. input := &s3.HeadObjectInput{}
  75. input.Bucket = &o.bucket.Name
  76. input.Key = &o.Key
  77. output, err := s3cli.HeadObject(context.Background(), input)
  78. if err != nil {
  79. log.Errorf("s3cli.HeadObject fail %s", err)
  80. return nil
  81. }
  82. ret := http.Header{}
  83. for k, v := range output.Metadata {
  84. if v != "" && len(v) > 0 {
  85. ret.Add(k, v)
  86. }
  87. }
  88. if output.CacheControl != nil && len(*output.CacheControl) > 0 {
  89. ret.Set(cloudprovider.META_HEADER_CACHE_CONTROL, *output.CacheControl)
  90. }
  91. if output.ContentType != nil && len(*output.ContentType) > 0 {
  92. ret.Set(cloudprovider.META_HEADER_CONTENT_TYPE, *output.ContentType)
  93. }
  94. if output.ContentDisposition != nil && len(*output.ContentDisposition) > 0 {
  95. ret.Set(cloudprovider.META_HEADER_CONTENT_DISPOSITION, *output.ContentDisposition)
  96. }
  97. if output.ContentEncoding != nil && len(*output.ContentEncoding) > 0 {
  98. ret.Set(cloudprovider.META_HEADER_CONTENT_ENCODING, *output.ContentEncoding)
  99. }
  100. if output.ContentLanguage != nil && len(*output.ContentLanguage) > 0 {
  101. ret.Set(cloudprovider.META_HEADER_CONTENT_LANGUAGE, *output.ContentLanguage)
  102. }
  103. return ret
  104. }
  105. func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
  106. return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta)
  107. }