获取对象属性

更新时间: 2019-03-14 10:05

您可以通过ObsClient.GetObjectMetadata来获取对象属性,包括对象长度,对象MIME类型,对象自定义元数据等信息。以下代码展示了如何获取对象属性:

  1. // 引入依赖包
  2. import (
  3. "fmt"
  4. "obs"
  5. )
  6.  
  7. var ak = "*** Provide your Access Key ***"
  8. var sk = "*** Provide your Secret Key ***"
  9. var endpoint = "https://your-endpoint"
  10.  
  11. // 创建ObsClient结构体
  12. var obsClient, _ = obs.New(ak, sk, endpoint)
  13.  
  14. func main() {
  15. input := &obs.GetObjectMetadataInput{}
  16. input.Bucket = "bucketname"
  17. input.Key = "objectkey"
  18. output, err := obsClient.GetObjectMetadata(input)
  19. if err == nil {
  20. fmt.Printf("StorageClass:%s\n", output.StorageClass)
  21. fmt.Printf("ETag:%s\n", output.ETag)
  22. fmt.Printf("ContentType:%s\n", output.ContentType)
  23. fmt.Printf("ContentLength:%d\n", output.ContentLength)
  24. fmt.Printf("Metadata:%v\n", output.Metadata)
  25. } else if obsError, ok := err.(obs.ObsError); ok {
  26. fmt.Printf("Code:%s\n", obsError.Code)
  27. fmt.Printf("Message:%s\n", obsError.Message)
  28. }
  29. }

父主题:管理对象