如何解组S3 XML在Go?

  • 本文关键字:Go XML 何解组 S3 go
  • 更新时间 :
  • 英文 :


我正在对s3桶的根目录进行httpGET请求以列出内容。我希望我的应用程序解析的内容,以了解什么是在桶。

XML是这样的:

<?xml
version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>my-bucket</Name>
<Contents>
<Key>one.json</Key>
</Contents>
<Contents>
<Key>three.json</Key>
</Contents>
<Contents>
<Key>two.json</Key>
</Contents>
</ListBucketResult>

解析它,我试着用这个:

type S3Content struct {
Key     string   `xml:"Key"`
}
type S3ListBucketResult struct {
Contents []S3Content `xml:"Contents"`
}
type HttpS3Response struct {
ListBucketResult S3ListBucketResult `xml:"ListBucketResult"`
}
resp, _ := http.Get("https://my-bucket.s3.amazonaws.example.com")
body, _ := ioutil.ReadAll(resp.Body)
var parsed HttpS3Response
xml.Unmarshal(body, &parsed)
fmt.Println(parsed.ListBucketResult.Contents)

然而Contents切片显示为空。知道我做错了什么吗?

噢,根密钥不包含在路径

type S3Content struct {
Key     string   `xml:"Key"`
}
type HttpS3Response struct {
Contents []S3Content `xml:"Contents"`
}
resp, _ := http.Get("https://my-bucket.s3.amazonaws.example.com")
body, _ := ioutil.ReadAll(resp.Body)
var parsed HttpS3Response
xml.Unmarshal(body, &parsed)
fmt.Println(parsed.Contents)

最新更新