将S3 URI解析为Go中的存储桶和键

huangapple go评论71阅读模式
英文:

Parsing S3 URI into bucket and key in Go

问题

在官方的AWS Go SDK中似乎没有一个函数可以解析类似s3://格式的URI字符串(例如s3://mybucket/some/place/on/there.gz)并提供桶(bucket)和键(key)等信息。Java SDK中有一个类似的函数。我是否忽略了Go SDK没有这样的明显原因?

这个问题涉及到保持兼容性。一个SDK函数提供了一个明确的方法来正确解析S3地址,并避免了url.Parse()等函数可能忽略的细微情况的担忧。

英文:

There doesn't seem to be a function in the official AWS Go SDK that'll parse an s3:// style URI-like string (i.e. s3://mybucket/some/place/on/there.gz) and provide things like the bucket and key. The Java SDK has such a function. Am I missing an obvious reason why the Go SDK does not?

This question is about maintaining compatibility. An SDK function provides a clear authority on how to properly parse S3 addresses and deters worry over subtle cases that url.Parse() and ilk may miss.

答案1

得分: 14

尽管只有AWS开发人员能回答实际问题(为什么这不包含在SDK中),但有一个简单的答案:S3 URL是URL,你可以使用net/url来解析它们:

u, _ := url.Parse("s3://mybucket/some/place/on/there.gz")
fmt.Printf("proto: %q, bucket: %q, key: %q", u.Scheme, u.Host, u.Path)

这里是Playground

英文:

Though only the AWS devs can answer the actual question (why isn't this included in the SDK), there is the simple answer: S3 URLs are URLs, and you can use net/url to parse them:

u,_ := url.Parse("s3://mybucket/some/place/on/there.gz")
fmt.Printf("proto: %q, bucket: %q, key: %q", u.Scheme, u.Host, u.Path)

Playground here

huangapple
  • 本文由 发表于 2017年6月29日 02:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/44809931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定