英文:
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论