Skip to content

Commit

Permalink
Fix: Correct google stow item url format (#17)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Graetz <[email protected]>
  • Loading branch information
fg91 authored Dec 18, 2024
1 parent 632adef commit fcb9bf9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
1 change: 1 addition & 0 deletions google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

// Kind represents the name of the location/storage type.
const Kind = "google"
const Protocol = "gs"

const (
// The service account json blob
Expand Down
5 changes: 1 addition & 4 deletions google/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ func merge(metadata ...map[string]string) map[string]string {
}

func (c *Container) convertToStowItem(attr *storage.ObjectAttrs) (stow.Item, error) {
u, err := prepUrl(attr.MediaLink)
if err != nil {
return nil, err
}
u := prepUrl(attr)

mdParsed, err := parseMetadata(attr.Metadata)
if err != nil {
Expand Down
20 changes: 13 additions & 7 deletions google/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,21 @@ func (i *Item) StorageObject() *storage.ObjectAttrs {
return i.object
}

// prepUrl takes a MediaLink string and returns a url
func prepUrl(str string) (*url.URL, error) {
u, err := url.Parse(str)
if err != nil {
return nil, err
// prepUrl takes ObjectAttrs and returns a url constructed from the bucket and name.
// We don't use attr.MediaLink because it is in the format
// `google://storage.googleapis.com/download/storage/v1/b/some-bucket/...` instead of
// `gs://some-bucket/...`.
// Otherwise, when using stow to list a bucket `gs://...`, the resulting objects
// `google://storage.googleapis.com/download/storage/v1/b/...` could not be found
// using the same stow client.
func prepUrl(attr *storage.ObjectAttrs) *url.URL {
u := url.URL{
Scheme: Protocol,
Host: attr.Bucket,
Path: attr.Name,
}
u.Scheme = "google"

// Discard the query string
u.RawQuery = ""
return u, nil
return &u
}
40 changes: 26 additions & 14 deletions google/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,34 @@ func (l *Location) RemoveContainer(id string) error {
// ItemByURL retrieves a stow.Item by parsing the URL, in this
// case an item is an object.
func (l *Location) ItemByURL(url *url.URL) (stow.Item, error) {
if url.Scheme != Kind {
return nil, errors.New("not valid google storage URL")
}
if url.Scheme == Kind {
// Url in the form: google://storage.googleapis.com/download/storage/v1/b/some-bucket
pieces := strings.SplitN(url.Path, "/", 8)

// /download/storage/v1/b/stowtesttoudhratik/o/a_first%2Fthe%20item
pieces := strings.SplitN(url.Path, "/", 8)
c, err := l.Container(pieces[5])
if err != nil {
return nil, stow.ErrNotFound
}

c, err := l.Container(pieces[5])
if err != nil {
return nil, stow.ErrNotFound
}
i, err := c.Item(pieces[7])
if err != nil {
return nil, stow.ErrNotFound
}
return i, nil

i, err := c.Item(pieces[7])
if err != nil {
return nil, stow.ErrNotFound
}
} else if url.Scheme == Protocol {
// Url in the form: gs://some-bucket
c, err := l.Container(url.Host)
if err != nil {
return nil, stow.ErrNotFound
}

return i, nil
i, err := c.Item(url.Path)
if err != nil {
return nil, stow.ErrNotFound
}
return i, nil
} else {
return nil, errors.New("not valid google storage URL")
}
}

0 comments on commit fcb9bf9

Please sign in to comment.