MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / extractLocationAndBucket

Function extractLocationAndBucket

pkg/blobmanager/s3/backend.go:116–143  ·  view source on GitHub ↗

Extract the custom endpoint and the bucket name from the location string The location string can be either a bucket name or a URL i.e bucket-name or https://custom-domain/bucket-name

(creds *Credentials)

Source from the content-addressed store, hash-verified

114// The location string can be either a bucket name or a URL
115// i.e bucket-name or https://custom-domain/bucket-name
116func extractLocationAndBucket(creds *Credentials) (string, string, error) {
117 // Older versions of the credentials didn't have the location field
118 // and just the bucket name was stored in the bucket name field
119 if creds.BucketName != "" {
120 return "", creds.BucketName, nil
121 }
122
123 // Newer versions of the credentials have the location field which can contain the endpoint
124 // so we override the bucket and set the endpoint if needed
125 parsedLocation, err := url.Parse(creds.Location)
126 if err != nil {
127 return "", "", fmt.Errorf("failed to parse location: %w", err)
128 }
129
130 host := parsedLocation.Host
131 // It's a bucket name
132 if host == "" {
133 return "", creds.Location, nil
134 }
135
136 endpoint := fmt.Sprintf("%s://%s", parsedLocation.Scheme, host)
137 // It's a URL, extract bucket name from the path
138 if pathSegments := strings.Split(parsedLocation.Path, "/"); len(pathSegments) > 1 {
139 return endpoint, pathSegments[1], nil
140 }
141
142 return "", "", fmt.Errorf("the location doesn't contain a bucket name")
143}
144
145// Exists check that the artifact is already present in the repository
146func (b *Backend) Exists(ctx context.Context, digest string) (bool, error) {

Callers 2

NewBackendFunction · 0.85

Calls 1

ParseMethod · 0.80

Tested by 1