1024programmer Java GCS – Read text files directly from Google Cloud Storage into python

GCS – Read text files directly from Google Cloud Storage into python

I feel a bit stupid right now. I’ve been reading tons of documentation 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’. In this bucket there is a folder ‘temp_files_folder’ which contains two files, one named ‘test.txt’ .txt file and a .csv file called ‘test.csv’. Both files are just because I tried using both files but the result is the same.

The contents of the file are

hej
 San
 

I want to read it into python, just like I use locally

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

This makes

['hej', 'san']
 

I 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 >
 

How to get the actual string in the file?

1> Daniel Rosem..:


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)
 

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/772499

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索