Warm tip: This article is reproduced from stackoverflow.com, please click
google-cloud-platform google-cloud-storage

Downloading object from google cloud storage using 'get' method

发布于 2020-03-27 10:18:57

I am trying to download an object from a google cloud store bucket using the method 'get' as described here https://cloud.google.com/storage/docs/json_api/v1/objects/get

It says in there that it "Retrieves an object or its metadata"

I want to use the API to download the object as mentioned in the documentation but all i can get is a dict containing the metadata for the object.

I tried using the selfLink and mediaLink provided in the metadata but to no avail.

http_request=resource.objects().get(bucket='bucket-name',object='Object-name')
http_response=http_request.execute()

resource is a valid authenticated resource object as the other methods are working fine.

Now i know i can use the python client api to download the object but is there a way this can be done using the 'get' method ? Or am i missing something obvious(which is possible as i am new to gcp)

Questioner
WonderChild
Viewed
86
Alex Riquelme 2019-07-04 00:04

As you said, that get request can obtain the object or the metadata of an object inside of your bucket. Using the call that you shared it will get only the metadata, if you want to obtain the object, you have to specify a destination with the "-o" flag, the command should be like the following:

curl -X GET \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -o "[SAVE_TO_LOCATION]" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]?alt=media"

Bear in mind that if you want to generate a token you can generate it with the following command:

gcloud beta auth application-default print-access-token