6

I would like to use Google Cloud Storage Client Library Functions.

For that I have to import cloudstorage. To get the cloudstorage I download Google Cloud Storage client library.

I try to import cloudstorage using python -c "import cloudstorage". I get the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/fghavamian/Documents/PhD_work/Codes/python/convnet_gcloud/cloudstorage/__init__.py", line 20, in <module>
    from .api_utils import RetryParams
  File "/Users/fghavamian/Documents/PhD_work/Codes/python/convnet_gcloud/cloudstorage/api_utils.py", line 173
    except self.retriable_exceptions, e:
                                    ^
SyntaxError: invalid syntax

Am I missing something?

Tynan J
  • 69
  • 6
Fariborz Ghavamian
  • 809
  • 2
  • 11
  • 23
  • 4
    My guess: you're running Python 3, but that looks like Python 2 syntax (`except Exception, e` is no longer used) – Chris_Rands Feb 01 '18 at 10:14
  • 1
    @Chris_Rands; Yes, it's because of using Python3. When I use python 2 the error goes away. Yet there is another problem, now I get the following error `ImportError: No module named appengine.api`. – Fariborz Ghavamian Feb 01 '18 at 13:01
  • Possible duplicate of [import cloudstorage, ImportError: No module named google.appengine.api](https://stackoverflow.com/questions/48564834/import-cloudstorage-importerror-no-module-named-google-appengine-api) – VictorGGl Feb 08 '18 at 10:54
  • did you ever found a solution? – Manza Aug 18 '18 at 02:21
  • I opened an issue on the [github](https://github.com/GoogleCloudPlatform/appengine-gcs-client/issues/78), but it looks like [google-cloud-python](https://github.com/GoogleCloudPlatform/google-cloud-python) is the more up-to-date API for Cloud Storage. – matwilso Sep 23 '18 at 22:37

1 Answers1

3

As one of the comments says, this is an issue with using Python 3, where the syntax has changed. It looks like the Google Cloud Storage library you are using is not as well supported and that the more recommended library is google-cloud-python.

You can install it with the command below, assuming you have installed the Google Cloud SDK:

pip install google-cloud-storage

Here is some sample code (from the docs) that shows how to read and write to a bucket.

from google.cloud import storage
client = storage.Client()
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('bucket-id-here')
# Then do other things...
blob = bucket.get_blob('remote/path/to/file.txt')
print(blob.download_as_string())  # read content from bucket and print to stdout
blob.upload_from_string('New contents!')
blob2 = bucket.blob('remote/path/storage.txt')
blob2.upload_from_filename(filename='/local/path.txt')  # write from path.txt to storage.txt
matwilso
  • 2,924
  • 3
  • 17
  • 24