FAQ
Using the google-api-go-client bindings for GCS (here<http://godoc.org/code.google.com/p/google-api-go-client/storage/v1beta2>)
I see how to get a file's metadata (via ObjectsService.Get) but how do I
just download the file's actual contents? (note that "just get the file's
metadata, read the mediaLink property out of there and then fetch that
resource" isn't really a viable answer for me as the first fetch is
entirely redundant - a file's medialink is entirely determined by its
bucket and object name).

cheers!
Ian

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Andrew Gerrand at Apr 7, 2014 at 7:38 am

    On 7 April 2014 11:51, Ian Rose wrote:

    Using the google-api-go-client bindings for GCS (here<http://godoc.org/code.google.com/p/google-api-go-client/storage/v1beta2>)
    I see how to get a file's metadata (via ObjectsService.Get) but how do I
    just download the file's actual contents? (note that "just get the file's
    metadata, read the mediaLink property out of there and then fetch that
    resource" isn't really a viable answer for me as the first fetch is
    entirely redundant - a file's medialink is entirely determined by its
    bucket and object name).
    Why can't you just use the medialink? Why do you need to use the
    google-api-go-client to fetch the file if you know the link already?

    Andrew

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Rose at Apr 7, 2014 at 1:59 pm
    Doh. I think I accidentally posted a reply just to Andrew...

    I wrote something like: We can certainly do that, its just not quite as
    simple as "client.Get(<medialink>)" due to some URL escaping nuances that I
    learned about in this thread<https://plus.google.com/+IanRose/posts/Tzw3QZqEQZk>.
      We are currently using the function below which isn't a big deal to do, it
    just seemed strange to me that the google-api-go-client library had
    functions for just about all operations *except* (arguably) the most common
    one. So I wanted to make sure I wasn't overlooking something...


    func GetFileContents(client *http.Client, bucket string, filepath string)
    (io.ReadCloser, error) {
    urls :=
    "https://www.googleapis.com/storage/v1beta2/b/{bucket}/o/{object}?alt=media"

    // you have to do this little dance because (by default) net/url will
    normalize URL escaping, which means
    // that will undo any "/" -> "%2f" escaping that we need to do to escape
    the slashes in the filepath
    req, err := http.NewRequest("GET", urls, nil)
    if err != nil {
    return nil, fmt.Errorf("failed to create http.Request: %s", err)
    }

    req.URL.Path = strings.Replace(req.URL.Path, "{bucket}",
    url.QueryEscape(bucket), 1)
    req.URL.Path = strings.Replace(req.URL.Path, "{object}",
    url.QueryEscape(filepath), 1)
    req.URL.Opaque = "//" + req.URL.Host + req.URL.Path

    rsp, err := client.Do(req)
    if err != nil {
    rsp.Body.Close()
    return nil, fmt.Errorf("failed to make http request: %s", err)
    }

    if rsp.StatusCode != http.StatusOK {
    return nil, fmt.Errorf("failed to make http request: %d response",
    rsp.StatusCode)
    }

    return rsp.Body, nil
    }


    On Monday, April 7, 2014 3:37:56 AM UTC-4, Andrew Gerrand wrote:


    On 7 April 2014 11:51, Ian Rose <ianr...@gmail.com <javascript:>> wrote:

    Using the google-api-go-client bindings for GCS (here<http://godoc.org/code.google.com/p/google-api-go-client/storage/v1beta2>)
    I see how to get a file's metadata (via ObjectsService.Get) but how do I
    just download the file's actual contents? (note that "just get the file's
    metadata, read the mediaLink property out of there and then fetch that
    resource" isn't really a viable answer for me as the first fetch is
    entirely redundant - a file's medialink is entirely determined by its
    bucket and object name).
    Why can't you just use the medialink? Why do you need to use the
    google-api-go-client to fetch the file if you know the link already?

    Andrew
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrew Gerrand at Apr 8, 2014 at 4:53 am
    The google-api-go-client makes calls to JSON APIs. It doesn't really make
    sense to return binary data via a JSON API. It's a limitation of the design
    of these JSON discovery-based APIs.

    That workaround is necessary to work around that escape handling. Sorry
    about that, but we're in a bind there because of the Go 1 compatibility
    promise.

    Andrew

    On 7 April 2014 23:59, Ian Rose wrote:

    Doh. I think I accidentally posted a reply just to Andrew...

    I wrote something like: We can certainly do that, its just not quite as
    simple as "client.Get(<medialink>)" due to some URL escaping nuances that I
    learned about in this thread<https://plus.google.com/+IanRose/posts/Tzw3QZqEQZk>.
    We are currently using the function below which isn't a big deal to do, it
    just seemed strange to me that the google-api-go-client library had
    functions for just about all operations *except* (arguably) the most
    common one. So I wanted to make sure I wasn't overlooking something...


    func GetFileContents(client *http.Client, bucket string, filepath string)
    (io.ReadCloser, error) {
    urls := "
    https://www.googleapis.com/storage/v1beta2/b/{bucket}/o/{object}?alt=media
    "

    // you have to do this little dance because (by default) net/url will
    normalize URL escaping, which means
    // that will undo any "/" -> "%2f" escaping that we need to do to escape
    the slashes in the filepath
    req, err := http.NewRequest("GET", urls, nil)
    if err != nil {
    return nil, fmt.Errorf("failed to create http.Request: %s", err)
    }

    req.URL.Path = strings.Replace(req.URL.Path, "{bucket}",
    url.QueryEscape(bucket), 1)
    req.URL.Path = strings.Replace(req.URL.Path, "{object}",
    url.QueryEscape(filepath), 1)
    req.URL.Opaque = "//" + req.URL.Host + req.URL.Path

    rsp, err := client.Do(req)
    if err != nil {
    rsp.Body.Close()
    return nil, fmt.Errorf("failed to make http request: %s", err)
    }

    if rsp.StatusCode != http.StatusOK {
    return nil, fmt.Errorf("failed to make http request: %d response",
    rsp.StatusCode)
    }

    return rsp.Body, nil
    }


    On Monday, April 7, 2014 3:37:56 AM UTC-4, Andrew Gerrand wrote:


    On 7 April 2014 11:51, Ian Rose wrote:

    Using the google-api-go-client bindings for GCS (here<http://godoc.org/code.google.com/p/google-api-go-client/storage/v1beta2>)
    I see how to get a file's metadata (via ObjectsService.Get) but how do I
    just download the file's actual contents? (note that "just get the file's
    metadata, read the mediaLink property out of there and then fetch that
    resource" isn't really a viable answer for me as the first fetch is
    entirely redundant - a file's medialink is entirely determined by its
    bucket and object name).
    Why can't you just use the medialink? Why do you need to use the
    google-api-go-client to fetch the file if you know the link already?

    Andrew

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Rose at Apr 8, 2014 at 1:05 pm
    Makes sense. Like I said, mostly I just wanted to make sure I wasn't
    overlooking anything. It sounds like what we are doing now is "the right
    thing" so that's good to know.

    cheers
    - Ian

    On Tue, Apr 8, 2014 at 12:52 AM, Andrew Gerrand wrote:

    The google-api-go-client makes calls to JSON APIs. It doesn't really make
    sense to return binary data via a JSON API. It's a limitation of the design
    of these JSON discovery-based APIs.

    That workaround is necessary to work around that escape handling. Sorry
    about that, but we're in a bind there because of the Go 1 compatibility
    promise.

    Andrew

    On 7 April 2014 23:59, Ian Rose wrote:

    Doh. I think I accidentally posted a reply just to Andrew...

    I wrote something like: We can certainly do that, its just not quite as
    simple as "client.Get(<medialink>)" due to some URL escaping nuances that I
    learned about in this thread<https://plus.google.com/+IanRose/posts/Tzw3QZqEQZk>.
    We are currently using the function below which isn't a big deal to do, it
    just seemed strange to me that the google-api-go-client library had
    functions for just about all operations *except* (arguably) the most
    common one. So I wanted to make sure I wasn't overlooking something...


    func GetFileContents(client *http.Client, bucket string, filepath string)
    (io.ReadCloser, error) {
    urls := "
    https://www.googleapis.com/storage/v1beta2/b/{bucket}/o/{object}?alt=media
    "

    // you have to do this little dance because (by default) net/url will
    normalize URL escaping, which means
    // that will undo any "/" -> "%2f" escaping that we need to do to escape
    the slashes in the filepath
    req, err := http.NewRequest("GET", urls, nil)
    if err != nil {
    return nil, fmt.Errorf("failed to create http.Request: %s", err)
    }

    req.URL.Path = strings.Replace(req.URL.Path, "{bucket}",
    url.QueryEscape(bucket), 1)
    req.URL.Path = strings.Replace(req.URL.Path, "{object}",
    url.QueryEscape(filepath), 1)
    req.URL.Opaque = "//" + req.URL.Host + req.URL.Path

    rsp, err := client.Do(req)
    if err != nil {
    rsp.Body.Close()
    return nil, fmt.Errorf("failed to make http request: %s", err)
    }

    if rsp.StatusCode != http.StatusOK {
    return nil, fmt.Errorf("failed to make http request: %d response",
    rsp.StatusCode)
    }

    return rsp.Body, nil
    }


    On Monday, April 7, 2014 3:37:56 AM UTC-4, Andrew Gerrand wrote:


    On 7 April 2014 11:51, Ian Rose wrote:

    Using the google-api-go-client bindings for GCS (here<http://godoc.org/code.google.com/p/google-api-go-client/storage/v1beta2>)
    I see how to get a file's metadata (via ObjectsService.Get) but how do I
    just download the file's actual contents? (note that "just get the file's
    metadata, read the mediaLink property out of there and then fetch that
    resource" isn't really a viable answer for me as the first fetch is
    entirely redundant - a file's medialink is entirely determined by its
    bucket and object name).
    Why can't you just use the medialink? Why do you need to use the
    google-api-go-client to fetch the file if you know the link already?

    Andrew

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 7, '14 at 1:52a
activeApr 8, '14 at 1:05p
posts5
users2
websitegolang.org

2 users in discussion

Ian Rose: 3 posts Andrew Gerrand: 2 posts

People

Translate

site design / logo © 2022 Grokbase