获取自定义元数据
更新时间: 2019-03-14 10:05
下载对象成功后会返回对象的自定义元数据。以下代码展示了如何获取自定义元数据:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- // 上传对象,设置自定义元数据
- putObjectInput := &obs.PutObjectInput{}
- putObjectInput.Bucket = "bucketname"
- putObjectInput.Key = "objectkey"
- putObjectInput.Metadata = map[string]string{"property": "property-value"}
- _, err := obsClient.PutObject(putObjectInput)
- if err != nil {
- panic(err)
- }
- input := &obs.GetObjectInput{}
- input.Bucket = "bucketname"
- input.Key = "objectkey"
- // 下载对象
- output, err := obsClient.GetObject(input)
- if err == nil {
- defer output.Body.Close()
- // 获取对象自定义元数据
- fmt.Printf("Metadata:%v\n", output.Metadata)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:下载对象