Downloading a file from google cloud storage insid

2019-04-05 12:19发布

问题:

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.

回答1:

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:

# 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.