Downloading a file from google cloud storage inside a folder

后端 未结 1 675
悲哀的现实
悲哀的现实 2021-02-05 08:57

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

1条回答
  •  粉色の甜心
    2021-02-05 09:39

    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.

    0 讨论(0)
提交回复
热议问题