1

I'm having issues downloading files on my google bucket I followed this Google tutorial(using a client library & service account)

Here is the python code I use (from google example) : import json from httplib2 import Http from oauth2client.client import SignedJwtAssertionCredentials from apiclient.discovery import build

client_email = 'myclientname@myproject.iam.gserviceaccount.com'

json_file = 'resources/Google/myproject-anumber.json' ## <---- JSON provided when i created the service account

cloud_storage_bucket = 'mybucketname'

report_to_download = 'path/to/my/file.zip'


private_key = json.loads(open(json_file).read())['private_key']

credentials = SignedJwtAssertionCredentials(client_email, private_key, 
              'https://www.googleapis.com/auth/devstorage.read_only')

storage = build('storage', 'v1', http=credentials.authorize(Http()))
report = storage.objects().get(bucket = cloud_storage_bucket, object = 
report_to_download).execute()

I thought i would have then to output 'report' to a file, but no, as described here, report is only a dict object :

https://google-api-client-libraries.appspot.com/documentation/storage/v1beta2/python/latest/storage_v1beta2.objectAccessControls.html#get

I tried then to use the selfLink attribute or mediaLink, but with no success. Same for 'https://cloud.google.com/storage/docs/json_api/v1/objects/get' which returns the ACL aswell

Thank you in advance,

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Elerir
  • 31
  • 1
  • 1
  • 8

1 Answers1

4

You could download an object with the code shown here. I suggest to follow this document to install the Cloud Storage client libraries and set up the authentication.

Below is a Python code sample to download an object, based on the aforementioned information.

from google.cloud import storage
if __name__ == '__main__':
    bucket_name = 'your_bucket'
    source_blob_name = 'your_object'
    destination_file_name = 'local_file'
    #DOWNLOAD
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)
    print('Blob {} downloaded to {}.'.format(source_blob_name, destination_file_name))
F10
  • 2,843
  • 2
  • 12
  • 18
  • 1
    That means the method described https://support.google.com/googleplay/android-developer/answer/6135870?visit_id=636715859850203138-3634819881&p=financial_export&rd=1#export is deprecated then ? I didnt want to use gsutil – Elerir Sep 07 '18 at 09:02
  • That `get` method returns the object metadata. If you want to download an object, you should use the [client libraries](https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python), I'm editing my answer with a code sample to download an object with the Python client libraries. – F10 Sep 07 '18 at 12:58
  • Thank you, it wasnt really written that it was only the metadata ! I thought i could download this file querying one more time the api – Elerir Sep 17 '18 at 15:36