1
votes

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 = '[email protected]'

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,

1

1 Answers

4
votes

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