object_acl.go 963 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // ObjectACLSample shows how to set and get object ACL
  8. func ObjectACLSample() {
  9. // Create bucket
  10. bucket, err := GetTestBucket(bucketName)
  11. if err != nil {
  12. HandleError(err)
  13. }
  14. // Create object
  15. err = bucket.PutObject(objectKey, strings.NewReader("YoursObjectValue"))
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // Case 1: Set bucket ACL, valid ACLs are ACLPrivate、ACLPublicRead、ACLPublicReadWrite
  20. err = bucket.SetObjectACL(objectKey, oss.ACLPrivate)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // Get object ACL, returns one of the three values: private、public-read、public-read-write
  25. goar, err := bucket.GetObjectACL(objectKey)
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. fmt.Println("Object ACL:", goar.ACL)
  30. // Delete object and bucket
  31. err = DeleteTestBucketAndObject(bucketName)
  32. if err != nil {
  33. HandleError(err)
  34. }
  35. fmt.Println("ObjectACLSample completed")
  36. }