Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Correct google stow item url format #17

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

@fg91 fg91 Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapting this function was required for google/stow_test.go to pass.

Note that on master and on this branch I needed to disable this check for the test to pass tough: t.Fatalf("Leaked %d TCP file descriptor", diffFDs).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. Can you paste the error? I'm unable to repro it on a Mac (I also added #18 and all tests pass on linux/amd64 too).

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")
}
}