object_meta.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // ObjectMetaSample shows how to get and set the object metadata
  8. func ObjectMetaSample() {
  9. // Create bucket
  10. bucket, err := GetTestBucket(bucketName)
  11. if err != nil {
  12. HandleError(err)
  13. }
  14. // Delete object
  15. err = bucket.PutObject(objectKey, strings.NewReader("YoursObjectValue"))
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // Case 0: Set bucket meta. one or more properties could be set
  20. // Note: Meta is case insensitive
  21. options := []oss.Option{
  22. oss.Expires(futureDate),
  23. oss.Meta("myprop", "mypropval")}
  24. err = bucket.SetObjectMeta(objectKey, options...)
  25. if err != nil {
  26. HandleError(err)
  27. }
  28. // Case 1: Get the object metadata. Only return basic meta information includes ETag, size and last modified.
  29. props, err := bucket.GetObjectMeta(objectKey)
  30. if err != nil {
  31. HandleError(err)
  32. }
  33. fmt.Println("Object Meta:", props)
  34. // Case 2: Get all the detailed object meta including custom meta
  35. props, err = bucket.GetObjectDetailedMeta(objectKey)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. fmt.Println("Expires:", props.Get("Expires"))
  40. // Case 3: Get the object's all metadata with contraints. When constraints are met, return the metadata.
  41. props, err = bucket.GetObjectDetailedMeta(objectKey, oss.IfUnmodifiedSince(futureDate))
  42. if err != nil {
  43. HandleError(err)
  44. }
  45. fmt.Println("MyProp:", props.Get("X-Oss-Meta-Myprop"))
  46. _, err = bucket.GetObjectDetailedMeta(objectKey, oss.IfModifiedSince(futureDate))
  47. if err == nil {
  48. HandleError(err)
  49. }
  50. goar, err := bucket.GetObjectACL(objectKey)
  51. if err != nil {
  52. HandleError(err)
  53. }
  54. fmt.Println("Object ACL:", goar.ACL)
  55. // Delete object and bucket
  56. err = DeleteTestBucketAndObject(bucketName)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. fmt.Println("ObjectMetaSample completed")
  61. }