GCS - read text files from Google Cloud Storage directly into python


Digestion:

I'm a little silly now. I've been reading tons of documentation and stackoverflow questions but I'm getting it right.

I have a file on Google Cloud Storage. It's in the bucket "test_bucket". In this bucket, there is a folder "temp_files_folder" which contains two files, a .txt file named "test.txt" and a .csv file named "test.csv". The two files are just because I try to use both, but the result is the same both ways.

The content of the file is

hej
san

I want to read it into python as if using locally

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

this makes

['hej', 'san']

I try to use

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 in the file?

Daniel Rothman:

download_as_stringis a method, you need to call it.

print(blob.download_as_string())

You'd more likely want to assign it to a variable so you download it once, then you can print it and do anything else with it:

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

Related


Python: read all files as gcs_uri in google cloud storage

low I'm new to google cloud platform and I have this problem: In my google bucket, I have 5 folders, each folder contains 100 audio files (.wav), and I want to access each folder , then convert speech to -text. I managed to get the second part done using googl

How to read Google Cloud Storage files from Google App Engine

Ali Khosro How to open files (from App Engine) stored in Google Cloud Storage? I am using Python, Flask and App Engine (flexible environment). The file is not public, the bucket belongs to an App Engine project, so the correct permissions are set. app.yaml run

How to read Google Cloud Storage files from Google App Engine

Ali Khosro How to open files (from App Engine) stored in Google Cloud Storage? I am using Python, Flask and App Engine (flexible environment). The file is not public, the bucket belongs to an App Engine project, so the correct permissions are set. app.yaml run

How to read Google Cloud Storage files from Google App Engine

Ali Khosro How to open files (from App Engine) stored in Google Cloud Storage? I am using Python, Flask and App Engine (flexible environment). The file is not public, the bucket belongs to an App Engine project, so the correct permissions are set. app.yaml run

Is it possible to upload files directly to Google Cloud Storage?

Dominic Weber I have written a file upload for a larger file which is uploading the file to Cloud Storage. Unfortunately, this takes a while as the file is uploaded to the web server and then from the web server to Google Cloud Storage. Is it possible to uploa

Is it possible to upload files directly to Google Cloud Storage?

Dominic Weber I have written a file upload for a larger file which is uploading the file to Cloud Storage. Unfortunately, this takes a while as the file is uploaded to the web server and then from the web server to Google Cloud Storage. Is it possible to uploa

Is it possible to upload files directly to Google Cloud Storage?

Dominic Weber I have written a file upload for a larger file which is uploading the file to Cloud Storage. Unfortunately, this takes a while as the file is uploaded to the web server and then from the web server to Google Cloud Storage. Is it possible to uploa

Is it possible to upload files directly to Google Cloud Storage?

Dominic Weber I have written a file upload for a larger file which is uploading the file to Cloud Storage. Unfortunately, this takes a while as the file is uploaded to the web server and then from the web server to Google Cloud Storage. Is it possible to uploa

Is it possible to upload files directly to Google Cloud Storage?

Dominic Weber I have written a file upload for a larger file which is uploading the file to Cloud Storage. Unfortunately, this takes a while as the file is uploaded to the web server and then from the web server to Google Cloud Storage. Is it possible to uploa

Is it possible to upload files directly to Google Cloud Storage?

Dominic Weber I have written a file upload for a larger file which is uploading the file to Cloud Storage. Unfortunately, this takes a while as the file is uploaded to the web server and then from the web server to Google Cloud Storage. Is it possible to uploa

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Put files into Google Cloud Storage (GCS) via signed URL

tea soup I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud C

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co

Put files into Google Cloud Storage (GCS) via signed URL

kctang: I'm trying to use a "signed URL" to access/upload files to Google Cloud Storage (GCS). Follow the instructions at https://developers.google.com/storage/docs/accesscontrol#Signing-Strings what did I do: "Web Application" is registered in Google Cloud Co