Reputation: 7677
I am making an http GET
request on the root directory of my s3 bucket to list the contents. I would like my application to parse the contents to understand what's in the bucket.
The XML looks like this:
<?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>
To parse it I am trying to use this:
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)
However the Contents
slice appears empty. Any idea what I am doing wrong?
Upvotes: 2
Views: 117
Reputation: 7677
Oh, the root key isn't included in the path
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)
Upvotes: 2