Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file from google cloud storage inside a folder

I've got a python script that gets a list of files that have been uploaded to a google cloud storage bucket, and attempts to retrieve the data as a string.

The code is simply:

file = open(base_dir + "/" + path, 'wb') data =  Blob(path, bucket).download_as_string() file.write(data) 

My issue is that the data I've uploaded is stored inside folders in the bucket, so the path would be something like:

folder/innerfolder/file.jpg 

When the google library attempts to download the file, it gets it in the form of a GET request, which turns the above path into:

https://www.googleapis.com/storage/v1/b/bucket/o/folder%2Finnerfolder%2Ffile.jpg 

Is there any way to stop this happening / download the file though this way? Cheers.

like image 879
pyy Avatar asked Mar 02 '17 12:03

pyy


People also ask

How do I export from Google Cloud?

You can export your images using either the Google Cloud console, the Google Cloud CLI, or the Cloud Build API. In the Google Cloud console, go to the Images page. Click the name of the image that you want to export to go to the image details page. You can't export public images provided by Google.

How do I download from the cloud console?

In your Cloud Shell Editor Explorer, right-click a directory or file and then click Copy Download Link, Download, or Upload Files. Alternatively, you can navigate to File > Download/Upload Files.


1 Answers

Yes - you can do this with the python storage client library.

Just install it with pip install --upgrade google-cloud-storage and then use the following code:

from google.cloud import storage  # Initialise a client storage_client = storage.Client("[Your project name here]") # Create a bucket object for our bucket bucket = storage_client.get_bucket(bucket_name) # Create a blob object from the filepath blob = bucket.blob("folder_one/foldertwo/filename.extension") # Download the file to a destination blob.download_to_filename(destination_file_name) 

You can also use .download_as_string() but as you're writing it to a file anyway downloading straight to the file may be easier.

The only slightly awkward thing to be aware of is that the filepath is the path from after the bucket name, so doesn't line up exactly with the path on the web interface.

like image 90
GregT Avatar answered Sep 20 '22 21:09

GregT