gsutil error: "module 'base64' has no attribute 'decodestring'"

Fri, Mar 5, 2021 2-minute read

gsutil is a command line tool in the google cloud sdk package that enables us interact with Google Cloud Storage Buckets. It’s great for scripting and general day-to-day tasks we don’t want to use the web interface for.

Problem

I was trying to set some IAM permissions the other day on a bucket and ran into yet another gsutil error:

➜  ~ gsutil iam ch serviceAccount:experimental@project-id.iam.gserviceaccount.com:roles/storage.admin gs://my-bucket/
Traceback (most recent call last):
  File "~/google-cloud-sdk/platform/gsutil/gsutil", line 21, in <module>
    gsutil.RunMain()
  File "~/google-cloud-sdk/platform/gsutil/gsutil.py", line 121, in RunMain
    import gslib.__main__
  File "~/google-cloud-sdk/platform/gsutil/gslib/__main__.py", line 93, in <module>
    from gslib.command_runner import CommandRunner
  File "~/google-cloud-sdk/platform/gsutil/gslib/command_runner.py", line 64, in <module>
    from gslib.tests.util import HAS_NON_DEFAULT_GS_HOST
  File "~/google-cloud-sdk/platform/gsutil/gslib/tests/util.py", line 126, in <module>
    TEST_ENCRYPTION_KEY1_SHA256_B64 = Base64Sha256FromBase64EncryptionKey(
  File "~/google-cloud-sdk/platform/gsutil/gslib/utils/encryption_helper.py", line 152, in Base64Sha256FromBase64EncryptionKey
    decoded_bytes = base64.decodestring(csek_encryption_key)
AttributeError: module 'base64' has no attribute 'decodestring'

This isn’t the first time I’ve run into an issue with gsutil and a Python version problem. In my last post on the subject, the solution was to set an environment variable and luckily for us, the same method applies here.

Solution

Identifying the problem to be caused by using Python 3, we set our environment shell to use Python 2.7. Run the following command in your shell to set the variable.

export CLOUDSDK_PYTHON=python2.7

Once set rerun your command and watch it go!

If you want to remove that environment variable you can also run the following command.

unset CLOUDSDK_PYTHON

Now your shell environment is how it started!


I hope this post helps you out, feel free to drop me a line on twitter with any feedback.