| 1 |
--- |
| 2 |
title: "URLs" |
| 3 |
date: 2019-05-06T09:55:09-07:00 |
| 4 |
weight: 2 |
| 5 |
--- |
| 6 |
|
| 7 |
In addition to creating portable types via provider-specific constructors |
| 8 |
(e.g., creating a `*blob.Bucket` using [`s3blob.OpenBucket`][]), many portable types |
| 9 |
can also be created using a URL. The scheme of the URL specifies the provider, |
| 10 |
and each provider implementation has code to convert the URL into the data |
| 11 |
needed to call its constructor. For example, calling |
| 12 |
`blob.OpenBucket("s3blob://my-bucket")` will return a `*blob.Bucket` created |
| 13 |
using [`s3blob.OpenBucket`][]. |
| 14 |
|
| 15 |
[`s3blob.OpenBucket`]: https://godoc.org/gocloud.dev/blob/s3blob#OpenBucket |
| 16 |
|
| 17 |
<!--more--> |
| 18 |
|
| 19 |
Each portable API package will document the types that it supports opening |
| 20 |
by URL. For example, the `blob` package supports `Bucket`s, while the `pubsub` |
| 21 |
package supports `Topic`s and `Subscription`s. Each provider implementation will |
| 22 |
document what scheme(s) it registers for, and what format of URL it expects. |
| 23 |
|
| 24 |
Each portable type URL opener will accept URL schemes with an `<api>+` prefix |
| 25 |
(e.g. `blob+file:///dir` instead of `file:///dir`, as well as schemes with an |
| 26 |
`<api>+<type>+` prefix (e.g. `blob+bucket+file:///dir`). |
| 27 |
|
| 28 |
Each portable API package should include an example using a URL, and |
| 29 |
many providers will include provider-specific examples as well. |
| 30 |
|
| 31 |
## Muxes |
| 32 |
|
| 33 |
Each portable type that is openable via URL will have a top-level function |
| 34 |
you can call, like [`blob.OpenBucket`][]. This top-level function uses a default |
| 35 |
instance of a `URLMux` multiplexer to map schemes to a provider-specific |
| 36 |
opener for the type. For example, `blob` has a [`BucketURLOpener`][] interface |
| 37 |
that providers implement and then register using [`RegisterBucket`][] on the |
| 38 |
result of [`DefaultURLMux`][]. |
| 39 |
|
| 40 |
Many applications will work just fine using the default mux through the |
| 41 |
top-level `Open` functions. However, if you want more control, you can create |
| 42 |
your own `URLMux` and register the provider `URLOpener`s you need. Most |
| 43 |
providers will export URLOpeners that give you more fine grained control over |
| 44 |
the arguments needed by the constructor. In particular, portable types opened |
| 45 |
via URL will often use default credentials from the environment. For example, |
| 46 |
the AWS URL openers use the credentials saved by "aws login" (we don't want |
| 47 |
to include credentials in the URL itself, since they are likely to be |
| 48 |
sensitive). |
| 49 |
|
| 50 |
1. Instantiate the provider's `URLOpener` with the specific fields you need. |
| 51 |
For example, `s3blob.URLOpener{ConfigProvider: myAWSProvider}` using a |
| 52 |
`ConfigProvider` that holds explicit AWS credentials. |
| 53 |
2. Create your own instance of the `URLMux`. For example: `mymux := new(blob.URLMux)` |
| 54 |
3. Register your custom URLOpener on your mux. For example: |
| 55 |
`mymux.RegisterBucket(s3blob.Scheme, myS3URLOpener)` |
| 56 |
4. Now use your mux to open URLs: `mymux.OpenBucket("s3://my-bucket")` |
| 57 |
|
| 58 |
[`blob.OpenBucket`]: https://godoc.org/gocloud.dev/blob#OpenBucket |
| 59 |
[`BucketURLOpener`]: https://godoc.org/gocloud.dev/blob#BucketURLOpener |
| 60 |
[`DefaultURLMux`]: https://godoc.org/gocloud.dev/blob#DefaultURLMux |
| 61 |
[`RegisterBucket`]: https://godoc.org/gocloud.dev/blob#URLMux.RegisterBucket |