0

I am trying to read a file in stored in Google Cloud Storage bucket python:

textfile = open("${gcs_bucket}mdm/OFF-B/test.txt", 'r') 
times = textfile.read().splitlines() 
textfile.close() 
print(getcwd()) 
print(times)

The file is present in that location but I am receiving the following error:

File "/var/cache/tomcat/temp/interpreter-9196592956267519250.tmp", line 3, in <module>
  textfile = open("gs://tp-bi-datalake-mft-landing-dev/mdm/OFF-B/test.txt", 'r')
IOError: [Errno 2] No such file or directory: 'gs://tp-bi-datalake-mft-landing-dev/mdm/OFF-B/test.txt'
Pol Ortiz
  • 461
  • 6
  • 14
Ria Chaudhuri
  • 11
  • 1
  • 1

1 Answers1

3

That's because you are trying to read it as a local file.

To read from Cloud Storage you need to import the library and use the client.

Check this similar Stackoverflow Question.

In your case it would be something like:

from google.cloud import storage

# Instantiates a client
client = storage.Client()

bucket_name = 'tp-bi-datalake-mft-landing-dev'

bucket = client.get_bucket(bucket_name)

blob = bucket.get_blob('mdm/OFF-B/test.txt')

downloaded_blob = blob.download_as_string()

print(downloaded_blob)

Also you will need to install the library, you can do that simply by running:

pip install google-cloud-storage before you run your code.

Also here you can find some more Google Cloud Storage Python Samples.

TasosZG
  • 1,274
  • 1
  • 6
  • 13