object_acl.go 987 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // ObjectACLSample 展示了如何设置、读取文件权限(object acl)
  8. func ObjectACLSample() {
  9. // 创建Bucket
  10. bucket, err := GetTestBucket(bucketName)
  11. if err != nil {
  12. HandleError(err)
  13. }
  14. // 创建object
  15. err = bucket.PutObject(objectKey, strings.NewReader("YoursObjectValue"))
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // 场景:设置Bucket ACL,可选权限有ACLPrivate、ACLPublicRead、ACLPublicReadWrite
  20. err = bucket.SetObjectACL(objectKey, oss.ACLPrivate)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // 查看Object ACL,返回的权限标识为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. // 删除object和bucket
  31. err = DeleteTestBucketAndObject(bucketName)
  32. if err != nil {
  33. HandleError(err)
  34. }
  35. fmt.Println("ObjectACLSample completed")
  36. }