Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!