object.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 ksyun
  15. import (
  16. "context"
  17. "net/http"
  18. "github.com/ks3sdklib/aws-sdk-go/service/s3"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/errors"
  21. )
  22. type SObject struct {
  23. bucket *SBucket
  24. cloudprovider.SBaseCloudObject
  25. }
  26. func (o *SObject) GetIBucket() cloudprovider.ICloudBucket {
  27. return o.bucket
  28. }
  29. func (o *SObject) GetAcl() cloudprovider.TBucketACLType {
  30. svc := o.bucket.region.getS3Client()
  31. input := &s3.GetObjectACLInput{
  32. Bucket: &o.bucket.Name,
  33. Key: &o.Key,
  34. }
  35. output, err := svc.GetObjectACL(input)
  36. if err != nil {
  37. return cloudprovider.ACLPrivate
  38. }
  39. return cloudprovider.TBucketACLType(s3.GetCannedACL(output.Grants))
  40. }
  41. func (o *SObject) SetAcl(acl cloudprovider.TBucketACLType) error {
  42. svc := o.bucket.region.getS3Client()
  43. aclStr := string(acl)
  44. input := &s3.PutObjectACLInput{
  45. Bucket: &o.bucket.Name,
  46. Key: &o.Key,
  47. ACL: &aclStr,
  48. }
  49. _, err := svc.PutObjectACL(input)
  50. if err != nil {
  51. return errors.Wrap(err, "PutObjectACL")
  52. }
  53. return nil
  54. }
  55. func (o *SObject) GetMeta() http.Header {
  56. svc := o.bucket.region.getS3Client()
  57. input := &s3.GetObjectInput{
  58. Bucket: &o.bucket.Name,
  59. Key: &o.Key,
  60. }
  61. output, err := svc.GetObject(input)
  62. if err != nil {
  63. return nil
  64. }
  65. meta := http.Header{}
  66. for k, v := range output.Metadata {
  67. meta.Add(k, *v)
  68. }
  69. return meta
  70. }
  71. func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
  72. return cloudprovider.ObjectSetMeta(ctx, o.bucket, o, meta)
  73. }