GCS - Read a text file from Google Cloud Storage directly into python

digestivee picture digestivee · Jan 16, 2018 · Viewed 26.3k times · Source

I feel kind of stupid right now. I have been reading numerous documentations and stackoverflow questions but I can't get it right.

I have a file on Google Cloud Storage. It is in a bucket 'test_bucket'. Inside this bucket there is a folder, 'temp_files_folder', which contains two files, one .txt file named 'test.txt' and one .csv file named 'test.csv'. The two files are simply because I try using both but the result is the same either way.

The content in the files is

hej
san

and I am hoping to read it into python the same way I would do on a local with

textfile = open("/file_path/test.txt", 'r')
times = textfile.read().splitlines()
textfile.close()
print(times)

which gives

['hej', 'san']

I have tried using

from google.cloud import storage

client = storage.Client()

bucket = client.get_bucket('test_bucket')

blob = bucket.get_blob('temp_files_folder/test.txt')

print(blob.download_as_string)

but it gives the output

<bound method Blob.download_as_string of <Blob: test_bucket, temp_files_folder/test.txt>>

How can I get the actual string(s) in the file?

Answer

Daniel Roseman picture Daniel Roseman · Jan 16, 2018

download_as_string is a method, you need to call it.

print(blob.download_as_string())

More likely, you want to assign it to a variable so that you download it once and can then print it and do whatever else you want with it:

downloaded_blob = blob.download_as_string()
print(downloaded_blob)
do_something_else(downloaded_blob)