登录
首页 >  Golang >  Go问答

Go 语言中如何解析 S3 XML 数据?

来源:stackoverflow

时间:2024-02-15 18:18:24 257浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《Go 语言中如何解析 S3 XML 数据?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在 s3 存储桶的根目录上发出 http get 请求以列出内容。我希望我的应用程序能够解析内容以了解存储桶中的内容。

xml 如下所示:



    my-bucket
    
        one.json
    
    
        three.json
    
    
        two.json
    

为了解析它,我尝试使用这个:

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)

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>