New constructs a cache configured using the provided URL string. URL should be of the form: "s3://region/bucket/optional-path-prefix". Credentials should be specified using one of the mechanisms supported by aws-sdk-go (see https://docs.aws.amazon.com/sdk-for-go/api/aws/session/).
(s string)
| 88 | // should be specified using one of the mechanisms supported by aws-sdk-go (see |
| 89 | // https://docs.aws.amazon.com/sdk-for-go/api/aws/session/). |
| 90 | func New(s string) (*cache, error) { |
| 91 | u, err := url.Parse(s) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | region := u.Host |
| 97 | path := strings.SplitN(strings.TrimPrefix(u.Path, "/"), "/", 2) |
| 98 | bucket := path[0] |
| 99 | var prefix string |
| 100 | if len(path) > 1 { |
| 101 | prefix = path[1] |
| 102 | } |
| 103 | |
| 104 | config := aws.NewConfig().WithRegion(region) |
| 105 | |
| 106 | // allow overriding some additional config options, mostly useful when |
| 107 | // working with s3-compatible services other than AWS. |
| 108 | if v := u.Query().Get("endpoint"); v != "" { |
| 109 | config = config.WithEndpoint(v) |
| 110 | } |
| 111 | if v := u.Query().Get("disableSSL"); v == "1" { |
| 112 | config = config.WithDisableSSL(true) |
| 113 | } |
| 114 | if v := u.Query().Get("s3ForcePathStyle"); v == "1" { |
| 115 | config = config.WithS3ForcePathStyle(true) |
| 116 | } |
| 117 | |
| 118 | sess, err := session.NewSession(config) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | |
| 123 | return &cache{ |
| 124 | S3: s3.New(sess), |
| 125 | bucket: bucket, |
| 126 | prefix: prefix, |
| 127 | }, nil |
| 128 | } |