Shikha
Shikha

Reputation: 361

Issue in Reading a file from gcs bucket through python script

I am trying to open a csv file which is present in gcs bucket to read the content and process the data accordingly in BigQuery. I am able to list out the csv's present in bucket , but not able to open it . My code is as below:

with cloudstorage.open(bucket_name/gs_file,"r+") as csvFile:
     reader = csv.reader(iter(csvFile.readline,''), delimiter = '|')
     csvfilearray = next(reader)
     print (csvfilearray)

Getting below error :

NameError: name 'cloudstorage' is not defined

Imported cloudstorage as below:

import cloudstorage as gcs

and tried with gcs.open again , But still error :

File "./GCS_Connection_Test.py", line 10, in <module>
    import cloudstorage as gcs
ImportError: No module named cloudstorage

Can anyone please help on how to open a file present on GCS for processing .

Upvotes: 0

Views: 2582

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39814

You're on the flexible environment according to your app.yaml file.

You used GoogleAppEngineCloudStorageClient, which is a standard environment library (just as appengine-gcs-client), you need to use the google-cloud-storage library, see Edit project configuration and install dependencies section in the Using Cloud Storage guide, which is the one you need to follow.

You also followed the standard environment instructions for installing the library, you need to use the flexible environment ones, see Using Python Libraries.

General note: look for the docs/flexible vs docs/standard strings in the doc page URLs to distinguish them (or the environment type displayed close to the top of the documentation left-side navigation bar, you may need to scroll up to see it)

Upvotes: 1

Related Questions